最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Compute the sum of elements of an array which can be null or undefined - Stack Overflow

programmeradmin1浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 8

You 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 ]
发布评论

评论列表(0)

  1. 暂无评论