简体   繁体   English

如何对相同键的值求和并在每个对象的对象数组中添加一个键,该键的值包含 id 相同键的数组?

[英]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.

相关问题 当键相同时如何更新对象数组中的对象值 - How to update an object value in array of objects when the keys are same 从数组中的键向对象数组 object 添加键和值 - Add key and value to array of objects object from the keys in array Javascript:合并具有不同键/值对的数组中的对象,并从相同的键中获取总和 - Javascript: Merge Objects in array with different key/value pairs, and getitng the sum from the same keys 将对象数组转换为单个 object,所有键的值都相同? - Turning an array of objects, into a single object with the same value for all keys? 如何在javascript中向相同的对象键添加相同的id值 - How to add same id value to equal object keys in javascript 如何在所有对象的键相同且值可能不同的情况下将对象数组转换为单个 object - How can I convert an array of objects into single object while keys of all the objects are same and value may differ 将计数值添加到具有相同键值的对象(对象数组的数组) - Add count value to object with same key values (array of array of objects) 数组对象的相同键的总和产生新的 object - sum of same keys of an array objects to produce new object 如何使用键数组在对象的对象中查找值 - How to find a value in object of objects with an array of keys Javascript - 如何创建对象数组,其中对象的键都具有相同的值 - Javascript - how to create an array of objects where the object's keys all have the same value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM