const n = this.item.answerSelfId.length; let num = 0; for (let m = 0 ; m < n ; m++) { if ( !this.item.answerSelfId[m]) { // 当前数组为empty时 this.item.answerSelfId[m] = ''; }else { this.item.answerSelfId[m] = this.item.answerSelfId[m].trim();// 去掉当前数组空串 } if ( this.item.answerSelfId[m] === '' || this.item.answerSelfId[m] === null) { num ++ ; // 获取数组中空串个数 } } if ( n === 0 || num === n) { this.item.answerSelfId = null; // 当数组中全部为空串时清除数组 }
效果
1.[empty,'22222'] 转化为['','2222']
2.['',' ']转化为null