我在 javascript 中有这个数组:你可以在这里看到
Array
(
[2012-10-01] => Array
(
[1] => 1
[2] => 0
...
[148] => 0
[149] => 0
[150] => 1
)
[2012-10-02] => Array
(
[1] => 0
[2] => 1
...
[148] => 0
[149] => 1
[150] => 0
)
[2012-10-03] => Array
(
[1] => 1
[2] => 0
...
[148] => 0
[149] => 0
[150] => 1
)
..............
为了减少它,我想只保留具有 1 的项目并省略具有 0 的项目。像这样
Array
(
[2012-10-01] => Array
(
[23] => 1
[64] => 1
[70] => 1
[73] => 1
[76] => 1
[108] => 1
[138] => 1
)
我使用了 underscorejs 和这段代码:
var new_o={};
var v = _.each(original_array,function(day,key){
var arr = {};
_.map(day,function(item,k){
if (item){
arr[k]=item;
}
}) ;
new_o[key]= arr;
} ) ;
它有效,但我很确定,我没有得到最好的下划线。有人可以提出更聪明的方法吗?