0

Seeking for help in a JavaScript problem. I am new to Nodejs, JavaScript. I am trying to merge duplicate values in the array if exists.

In below example - I have multiple asset_id which is repeated so I need to keep one and merge their values. I tried to do that but not getting the proper solution and output for the same.

const transaction = [{
    total_duration: 5,
    asset_id: 'ABC',
}, {
    total_duration: 15,
    asset_id: 'HGF',
}, {
    total_duration: 15,
    asset_id: 'XYZ',
}, {
    total_duration: 20,
    asset_id: 'XYZ',
}, {
    total_duration: 25,
    asset_id: 'DEF',
}, {
    total_duration: 20,
    asset_id: 'HGF',
}, {
    total_duration: 20,
    asset_id: 'HGF',
},
{
    total_duration: 10,
    asset_id: 'ABC',
}];

let newArr = [];
transaction.forEach(function (obj, ind, arr) {
    if (ind === arr.length - 1 || obj.asset_id !== arr[ind + 1].asset_id) {
        newArr.push(obj);
    } else {
        arr[ind + 1].total_duration += obj.total_duration;
    }
});

console.log(newArr)
Krishna
  • 1
  • 1
  • by merging values, do you mean to sum all values that belongs to same id? – Deniz Karadağ Sep 22 '22 at 10:18
  • @DenizKaradağ yes I need to merge the value with respect to duplicate id. – Krishna Sep 22 '22 at 10:21
  • Output should be like this - [{ total_duration: 15, asset_id: 'ABC', }, { total_duration: 55, asset_id: 'HGF', }, { total_duration: 35, asset_id: 'XYZ', }, { total_duration: 25, asset_id: 'DEF', }] – Krishna Sep 22 '22 at 10:22
  • 1
    Does this answer your question? [How can I group an array of objects by key?](https://stackoverflow.com/questions/40774697/how-can-i-group-an-array-of-objects-by-key) – IT goldman Sep 22 '22 at 10:34
  • @ITgoldman NO, actually I need to sum all values that belongs to same id, and keep in the output as below : [{ total_duration: 15, asset_id: 'ABC', }, { total_duration: 55, asset_id: 'HGF', }, { total_duration: 35, asset_id: 'XYZ', }, { total_duration: 25, asset_id: 'DEF', }] – Krishna Sep 22 '22 at 10:36

2 Answers2

0

I call this "group array of objects by property using reduce then getting values of it"

const transaction = [{total_duration:5,asset_id:"ABC"},{total_duration:15,asset_id:"HGF"},{total_duration:15,asset_id:"XYZ"},{total_duration:20,asset_id:"XYZ"},{total_duration:25,asset_id:"DEF"},{total_duration:20,asset_id:"HGF"},{total_duration:20,asset_id:"HGF"},{total_duration:10,asset_id:"ABC"}];

console.log(Object.values(transaction.reduce(function(agg, item) {
  agg[item.asset_id] = agg[item.asset_id] || {
    asset_id: item.asset_id,
    total_duration: 0

  }
  agg[item.asset_id].total_duration += item.total_duration
  return agg;
}, {})))
.as-console-wrapper {max-height: 100% !important}
IT goldman
  • 14,885
  • 2
  • 14
  • 28
0

const transaction = [{
    total_duration: 5,
    asset_id: 'ABC',
}, {
    total_duration: 15,
    asset_id: 'HGF',
}, {
    total_duration: 15,
    asset_id: 'XYZ',
}, {
    total_duration: 20,
    asset_id: 'XYZ',
}, {
    total_duration: 25,
    asset_id: 'DEF',
}, {
    total_duration: 20,
    asset_id: 'HGF',
}, {
    total_duration: 20,
    asset_id: 'HGF',
},
{
    total_duration: 10,
    asset_id: 'ABC',
}];

const result = transaction.reduce((a, b) => {
      const id = b["asset_id"]
      const value = b["total_duration"]
      if (a[id]) {
            a[id] = a[id] + value
      return a 
      } else {
             a[id] = value 
                 return a
      }
}, {})
console.log(result)
Deniz Karadağ
  • 751
  • 3
  • 8