[英]Reduce arrays in objects that in big array JS
I have a nested array, consisting of a lot of objects (400 items).我有一个嵌套数组,由很多对象(400 个项目)组成。 That objects have an ID property and an incomes property.该对象具有 ID 属性和收入属性。 That incomes property is an array of objects (40 items) that have value and date.Question is, how can I iterate through that main array to sum all my small array values to one summary?该收入属性是一组具有值和日期的对象(40 个项目)。问题是,如何遍历该主数组以将所有小数组值相加到一个摘要?
[{id: 1-400, incomes:[{value: "String", date: "String"}]}]
And, for example, that summary will be the 3rd property of main array:并且,例如,该摘要将是主数组的第三个属性:
[{id: 1-400, incomes:[{value: "String", date: "String"}], summaryIncome: "number"}]
I can iterate for that 400 items with for in loop, but I don`t understand how can I iterate both for big 400 items and small 40 items arrays with one loop.我可以用 for in 循环迭代那 400 个项目,但我不明白如何用一个循环迭代大 400 个项目和小的 40 个项目数组。
for(let i in data){
data[i].incomes[i].value
}
So i
will be >40 and I`ll get undefined in incomes[i].value.所以i
会> 40并且我会在收入[i].value中未定义。 Please help :)请帮忙 :)
var a = [
{id: 1, incomes: [{value:1}, {value: 2}]},
{id: 2, incomes: [{value:2}, {value: 3}]}];
var b = a.map(item => ({
...item,
summaryIncome: item.incomes.reduce((t, income) =>
t += income.value, 0)
}));
console.log(b);
This should print:这应该打印:
{id: 1, incomes: Array(2), summaryIncome: 3}
{id: 2, incomes: Array(2), summaryIncome: 5}
Iterate over each object and calculate the sum, assigning it to a new key:迭代每个对象并计算总和,将其分配给一个新键:
let data = getData() data.forEach(obj => { obj.summaryIncome = obj.incomes.reduce((agg,{value}) => agg+=value, 0); }) console.log(data); function getData(){ return [ {id: 1, incomes: [{value:1, date:new Date()}, {value: 2, date:new Date()}]}, {id: 2, incomes: [{value:3, date:new Date()}, {value: 4, date:new Date()}]} ]; }
This assumes:这假设:
incomes
and value
key总有一个incomes
和value
关键reduce
to iterate over each value and collect an aggregate, agg
, which will hold the item's sum它使用reduce
迭代每个值并收集一个聚合agg
,它将保存项目的总和400 items is not a lot of items. 400 个项目并不是很多项目。 You should be able to easily iterate them;您应该能够轻松地迭代它们; however, your number of items and the number of values are not a uniform array;但是,您的项目数和值的数量不是统一的数组; it's not a 400x400, it's 400x40.它不是 400x400,而是 400x40。 You don't have the same number of ids as you do values for each id.您没有与为每个 id 设置值相同数量的 id。 So your i
is fine for your item loop, but because you have fewer values than items, you will reach an end.所以你的i
适合你的项目循环,但因为你的值比项目少,你会走到尽头。
Even if they were the same number, it doesn't seem there is anything that binds the observation to the number of incomes, so it would be strongly discouraged to only use one loop and/or variable for tracking the index.即使它们是相同的数字,似乎也没有任何东西将观察结果与收入数量联系起来,因此强烈建议不要仅使用一个循环和/或变量来跟踪指数。
For an example of an implementation that uses two loops, refer to the previous section, which uses forEach
and reduce
to iterate both dimensions of your data.有关使用两个循环的实现示例,请参阅上一节,其中使用forEach
和reduce
来迭代数据的两个维度。 Likewise, below modifies your original code that to demonstrate using two for-loops:同样,下面修改您的原始代码以使用两个 for 循环进行演示:
for(let item of data){
let sum = 0;
for(let {value} of item.incomes){
sum += value;
}
item.summaryIncome = sum
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.