JS判斷陣列中的物件的每一個值不能為空
阿新 • • 發佈:2022-03-23
方法一:使用every()函式,此函式不怎麼常用,想要了解更多請自查
//表格 evaluateData為表格的資料 <el-table id="out-table3" :data="evaluateData" stripe style="width: 100%"> <el-table-column prop="appraiseValve" label="評價專案"> </el-table-column> <el-table-column label="評價主體" > <el-table-column prop="score" label="園長評"> <template slot-scope="scope"> <el-input v-model="scope.row.score" label="請輸入分數" style="width: 100px;"> </el-input> </template> </el-table-column> </el-table-column> </el-table> //Vue 結構下的的data data(){ return{ evaluateData:[ ] } } //提交的事件 submit(){ //此方法內的this指向 var that = this let flag = that.evaluateData.every(item => !!item.score) if (!flag) { console.log('score分數不能為空!') return }else{ //不為空時執行的結果 } }
方法二:some()函式
使用 if (this.evaluateData.some((val) => val.score== '')) { this.$message({ type: "error", message: "請填寫分數,分數不能為空, }); return; }