我正在使用Alasql来读取Excel数据。问题是数组可能包含所有具有空值的键,我的问题是:如果所有键都具有空值,如何删除行或记录?
[
0:{Name:"ABC1",No:5,BalanceDue:5000,Notes1:null,Notes2:null,CurrencyId:"2",Date:"06/01/2018"}
1:{Name:"ABC2",No:6,BalanceDue:6000,Notes1:null,Notes2:null,CurrencyId:"3",Date:"05/02/2018"}
2:{Name:null,No:null,BalanceDue:null,Notes1:null,Notes2:null,CurrencyId:null,Date:null}
3:{Name:"ABC3",No:7,BalanceDue:7700,Notes1:null,Notes2:null,CurrencyId:"8",Date:"15/02/2018"}
4:{Name:null,No:null,BalanceDue:null,Notes1:null,Notes2:null,CurrencyId:null,Date:null}
5:{Name:null,No:null,BalanceDue:null,Notes1:null,Notes2:null,CurrencyId:null,Date:null}
6:{Name:null,No:null,BalanceDue:null,Notes1:null,Notes2:null,CurrencyId:null,Date:null}
]
[
0:{Name:"ABC1",No:5,BalanceDue:5000,Notes1:null,Notes2:null,CurrencyId:"2",Date:"06/01/2018"}
1:{Name:"ABC2",No:6,BalanceDue:6000,Notes1:null,Notes2:null,CurrencyId:"3",Date:"05/02/2018"}
2:{Name:"ABC3",No:7,BalanceDue:7700,Notes1:null,Notes2:null,CurrencyId:"8",Date:"15/02/2018"}
]
答案 0 :(得分:1)
您可以array#filter
使用array#every
来过滤掉所有值为null
的对象。
var data = [{Name:"ABC1",No:5,BalanceDue:5000,Notes1:null,Notes2:null,CurrencyId:"2",Date:"06/01/2018"},{Name:"ABC2",No:6,BalanceDue:6000,Notes1:null,Notes2:null,CurrencyId:"3",Date:"05/02/2018"},{Name:null,No:null,BalanceDue:null,Notes1:null,Notes2:null,CurrencyId:null,Date:null},{Name:"ABC3",No:7,BalanceDue:7700,Notes1:null,Notes2:null,CurrencyId:"8",Date:"15/02/2018"},{Name:null,No:null,BalanceDue:null,Notes1:null,Notes2:null,CurrencyId:null,Date:null},{Name:null,No:null,BalanceDue:null,Notes1:null,Notes2:null,CurrencyId:null,Date:null},{Name:null,No:null,BalanceDue:null,Notes1:null,Notes2:null,CurrencyId:null,Date:null}],
result = data.filter(o => !Object.keys(o).every(k => !o[k]));
console.log(result);