[英]how to sum value of same keys and add a key with value that contains array of id same keys, in array of objects for each object?
I have an array that contains these objects.我有一个包含这些对象的数组。 What I want is sum values with same key and add a key contains id same keys that were match toghther.
我想要的是对具有相同键的值求和,并添加一个包含 id 匹配的相同键的键。 For example,
例如,
var data = [
{id:1, name: "fieldA", value: 20, },
{id:2, name: "building", value: 20, },
{id:3, name: "fieldA", value: 20, },
{id:4, name: "building", value: 20, }
];
have to become something like it:必须变成这样:
var data = [
{id:1,idd:[1,3], name: "fieldA", value: 40, },
{id:2,idd:[2,4], name: "building", value: 40, },
];
i have used this code: but i cant reach what i told above!我已经使用了这个代码:但我无法达到我上面所说的! :
:
var data = [ {id:1,id2:[], name: "fieldA", value: 20, }, {id:2,id2:[], name: "building", value: 20, }, {id:3,id2:[], name: "fieldA", value: 20, }, {id:4,id2:[], name: "building", value: 20, }];
var result = Object.values(data.reduce((acc,{id,id2, name, value})=>{
acc[name] = acc[name] || {id,id2, name, value:0};
acc[name].value +=value;
acc[name].id2[id2.length]= acc[name].id
/* console.log(acc[name])
*/ return acc;
},{}));
console.log(result);
i decide use reduce and map as it can.我决定尽可能使用reduce和map。 thanks.
谢谢。
note: this code has inspired from this question:注意:这段代码的灵感来自这个问题:
check array of objects if have same key and sum value
检查对象数组是否具有相同的键和总和值
All you need to do is push the correct id
, which you have already extracted using destructuring.您需要做的就是推送正确的
id
,您已经使用解构提取了它。
Also since you do not want to add the individual value
s, you can just omit the +
sign to have the last value
.此外,由于您不想添加单个
value
s,您可以省略+
号以获得最后一个value
。
var data = [ {id:1,id2:[], name: "fieldA", value: 20, }, {id:2,id2:[], name: "building", value: 20, }, {id:3,id2:[], name: "fieldA", value: 20, }, {id:4,id2:[], name: "building", value: 20, }]; var result = Object.values(data.reduce((acc,{id,id2, name, value})=>{ acc[name] = acc[name] || {id,id2, name, value:0}; acc[name].value += value; acc[name].id2.push(id); return acc; },{})); console.log(result);
Use array.reduce
in javascript.在 JavaScript 中使用
array.reduce
。
Just parse the accumulator array to find the node with combination of name and value.只需解析累加器数组即可找到具有名称和值组合的节点。 If that combination is not found, push the current node with id of current node in
idd
key.如果未找到该组合,则在
idd
键中使用当前节点的 id 推送当前节点。 Or else push the id of current node to the idd
key of node from the accumulator array.或者将当前节点的 id 从累加器数组推送到节点的
idd
键。
var data = [ {id:1, name: "fieldA", value: 20, }, {id:2, name: "building", value: 20, }, {id:3, name: "fieldA", value: 20, }, {id:4, name: "building", value: 20, } ]; const output = data.reduce((acc, curr) => { const node = acc.find((item) => item.name === curr.name && item.value === curr.value); if (node) { node.idd.push(curr.id); node.value += curr.value; } else { acc.push({ ...curr, idd: [curr.id]}) } return acc; }, []); console.log(output)
it looks like you just need to replace your line看起来你只需要更换你的线路
acc[name].id2[id2.length]= acc[name].id
with和
acc[name].id2.push(id)
So this:所以这:
var data = [
{id:1,id2:[], name: "fieldA", value: 1, },
{id:2,id2:[], name: "building", value: 5, },
{id:3,id2:[], name: "fieldA", value: 21, },
{id:4,id2:[], name: "building", value: 85, }
];
var result = Object.values(data.reduce((acc,{id,id2, name, value})=>{
acc[name] = acc[name] || {id,id2, name, value:0};
acc[name].value +=value;
acc[name].id2.push(id)
return acc;
},{}));
console.log(result);
will output:将输出:
[
{ id: 1, id2: [ 1, 3 ], name: 'fieldA', value: 22 },
{ id: 2, id2: [ 2, 4 ], name: 'building', value: 90 }
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.