简体   繁体   中英

add values in multiple arrays in javascript

I have data like this:

data = [
     [{a: "b", value: 12}, {a: "bb", value: 39}, {a: "bb", value: 150}],
     [{a: "c", value: 15}, {a: "cc", value: 83}, {a: "ccc", value: 12}],
     [{a: "d", value: 55}, {a: "dd", value: 9}, {a: "dd", value: 1}]
   ]

I want to add each value in each array with same index, so the result would be like this:

[82, 131, 163]

so I tried to loop through data and get each array. then loop through each array again to get each object like this:

for(let i = 0; i < data.length; i++) {
  let eachArr = data[i]
  for(let j = 0; j < eachArr.length; j++){
    console.log(eachArr[j]);
  }
}

I am not sure how to add each value with same index and push it to new array

Use Array#reduce method with Array#forEach method.

// iterate over the array
data.reduce(function(arr, o) {
  // itrate over the inner array
  o.forEach(function(obj, i) {
    // define array index if not defined and add the property value
    arr[i] = (arr[i] || 0) + obj.value;
  });
  // return the array reference
  return arr;
  // set initial value as an empty array for holding the result
}, []) 

 var data = [ [{ a: "b", value: 12 }, { a: "bb", value: 39 }, { a: "bb", value: 150 }], [{ a: "c", value: 15 }, { a: "cc", value: 83 }, { a: "ccc", value: 12 }], [{ a: "d", value: 55 }, { a: "dd", value: 9 }, { a: "dd", value: 1 }] ]; console.log( data.reduce(function(arr, o) { o.forEach(function(v, i) { arr[i] = (arr[i] || 0) + v.value; }); return arr; }, []) ) 

 data = [ [{a: "b", value: 12}, {a: "bb", value: 39}, {a: "bb", value: 150}], [{a: "c", value: 15}, {a: "cc", value: 83}, {a: "ccc", value: 12}], [{a: "d", value: 55}, {a: "dd", value: 9}, {a: "dd", value: 1}] ]; // create an array with the appropriate size var result = new Array(data[0].length); // initialize it with 0s result.fill(0); data.forEach(function(sub){ for(var i = 0; i < sub.length; i++) result[i] += sub[i].value; }); console.log(result); 

Reduce might give you some performance, but doubling the forEach is easier for newbies to comprehend and thus maintain.

The initialization only really needs to happen once and since you're only using integers (and not doubles/floats), you can use the bitwise-OR to floor a number |0 (in place of ||0 ); speed varies, but typically in C languages it's faster. JavaScript could be slower based on implementation, but I offer this only as an alternative to the solutions that currently exist.

 let data = [ [{a: "b", value: 12}, {a: "bb", value: 39}, {a: "bb", value: 150}], [{a: "c", value: 15}, {a: "cc", value: 83}, {a: "ccc", value: 12}], [{a: "d", value: 55}, {a: "dd", value: 9}, {a: "dd", value: 1}] ]; let sum = []; data.forEach(arr=>{ arr.forEach((obj,i)=>{ sum[i] = (sum[i]|0) + obj.value; }) }); console.log(sum); 

  • arr is the first layer of elements in your data array
  • obj is each element-array's value; in this case they are objects
  • i is the index of the object currently in scope, which is important to use for the sum's index

Use two for loops and index to increment the value

 var data = [ [{a: "b", value: 12}, {a: "bb", value: 39}, {a: "bb", value: 150}], [{a: "c", value: 15}, {a: "cc", value: 83}, {a: "ccc", value: 12}], [{a: "d", value: 55}, {a: "dd", value: 9}, {a: "dd", value: 1}] ]; var result = []; data.forEach(function(ele,ind){ ele.forEach(function(innerEle,inde){ if(!result[inde]) result[inde] = 0; result[inde] += innerEle.value; }) }); console.log(result) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM