判断数组中值为empty
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
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步