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

I have the following array:myData = [[2, null, null, 12, 2],[0, 0, 10, 1, null],undefined];I want to pu

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 ]

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745469271a4629058.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信