如何从数组中删除空行?

时间:2018-03-05 13:04:47

标签: angular typescript

我正在使用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"} 
]

1 个答案:

答案 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);