判断数组中值为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

 

posted @   boyanh  阅读(1449)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示