简体   繁体   English

在javascript中的多个数组中添加值

[英]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. Array#reduce方法与Array#forEach方法一起使用。

// 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. 减少可能会给您带来一些性能,但是将forEach翻倍对于新手来说更容易理解和维护。

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 ); 初始化实际上只需要发生一次,并且由于您只使用整数(而不是双精度数/浮点数),因此可以使用按位或运算来对数字|0 (代替||0 )进行||0 speed varies, but typically in C languages it's faster. 速度各不相同,但通常在C语言中速度更快。 JavaScript could be slower based on implementation, but I offer this only as an alternative to the solutions that currently exist. 基于实现的方式,JavaScript可能会变慢,但是我仅以此为替代现有解决方案的方法。

 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 arrdata数组中元素的第一层
  • obj is each element-array's value; obj是每个元素数组的值; 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 i是当前作用域中对象的索引,对于总和的索引使用这一点很重要

Use two for loops and index to increment the value 使用两个for循环和索引来增加值

 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) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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