admin管理员组文章数量:1426943
I have the following array:
myData = [[2, null, null, 12, 2],
[0, 0, 10, 1, null],
undefined];
I want to pute the sum of each sub-array, so in my case the result should be an array of this form: result = [16, 11, 0]
. This means that null
and undefined
to be replaced by zeros.
My approach works fine if I don't have the last element, undefined
:
MyCtrl.sum = MyCtrl.myData.reduce(function (r, a) {
a.forEach(function (b, i) {
r[i] = (r[i] || 0) + b;
});
return r;
}, []);
I tried some ways to return zero if there is a null
or undefined
as a sub-array but I don't know how:
MyCtrl.sum = MyCtrl.myData.reduce(function (r, a) {
if(a) {
a.forEach(function (b, i) {
r[i] = (r[i] || 0) + b;
}); } else {
r[i] = 0;
}
return r;
}, []);
It says that 'i' is not defined on the else branch.
Do you know any solution to this?
I have the following array:
myData = [[2, null, null, 12, 2],
[0, 0, 10, 1, null],
undefined];
I want to pute the sum of each sub-array, so in my case the result should be an array of this form: result = [16, 11, 0]
. This means that null
and undefined
to be replaced by zeros.
My approach works fine if I don't have the last element, undefined
:
MyCtrl.sum = MyCtrl.myData.reduce(function (r, a) {
a.forEach(function (b, i) {
r[i] = (r[i] || 0) + b;
});
return r;
}, []);
I tried some ways to return zero if there is a null
or undefined
as a sub-array but I don't know how:
MyCtrl.sum = MyCtrl.myData.reduce(function (r, a) {
if(a) {
a.forEach(function (b, i) {
r[i] = (r[i] || 0) + b;
}); } else {
r[i] = 0;
}
return r;
}, []);
It says that 'i' is not defined on the else branch.
Do you know any solution to this?
Share Improve this question edited Feb 2, 2018 at 8:42 Leo Messi asked Feb 2, 2018 at 8:30 Leo MessiLeo Messi 6,25622 gold badges80 silver badges155 bronze badges 1- 2 Is the result from the first array really 14 or should it be 16? – rodrigogq Commented Feb 2, 2018 at 8:32
3 Answers
Reset to default 8You could map the values by checking the inner array and if it is not given, then take an array as default value.
For adding, you could use zero as default value in a reduce with zero as start value.
var array = [[2, null, null, 12, 2], [0, 0, 10, 1, null], undefined],
result = array.map(a => (a || []).reduce((s, v) => s + (v || 0), 0));
console.log(result);
You can try (using lodash):
var myData = [[2, null, null, 12, 2],
[0, 0, 10, 1, null],
undefined];
// Flatten array
var myDataMerged = _.filter([].concat.apply([], myData), (v) => {
return _.isNumber(v);
});
var sum = myDataMerged.reduce((a,b) => {
return a+b;
});
console.log(myDataMerged);
console.log(sum);
How about use .map
?
var myData = [
[2,null,null,12,2],
[0,0,10,1,null],
undefined
];
var sum = myData.map(function(item,index){
if(!item)return 0;
if(item.length <2)return item[0];
return item.reduce(function(i1,i2){return i1+i2});
});;
console.log(sum);//[ 16, 11, 0 ]
本文标签: javascriptCompute the sum of elements of an array which can be null or undefinedStack Overflow
版权声明:本文标题:javascript - Compute the sum of elements of an array which can be null or undefined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745469271a2659674.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论