-4

I have the following input

[
        {
            "id": "abc",
            "data1": 3,
            "data2": "test1",
        },
        {
            "id": "abc",
            "data1": 4,
            "data2": "test1",
        },
        {
            "id": "xyz",
            "data1": 2,
            "data2": "test2",
        }
]

I would like to parse this list, convert the data1 to list and add all the data1 with similar id into it like the following to create new list.

[

        {
            "id": "abc",
            "data1": [3,4],
            "data2": "test1",
        },
        {
            "id": "abc",
            "data1": [2],
            "data2": "test2",
        }
]

I have tried a few ways like using map/reduce but none of my solution worked.

br0ek
  • 21
  • 4
  • 1
    Please visit the [help], take the [tour] to see what and [ask]. Do some research - [search SO for answers](https://www.google.com/search?q=javascript+merge+objects+by+id+site%3Astackoverflow.com). If you get stuck, post a [mcve] of your attempt, noting input and expected output using the [\[<>\]](https://meta.stackoverflow.com/questions/358992/ive-been-told-to-create-a-runnable-example-with-stack-snippets-how-do-i-do) snippet editor. – mplungjan Nov 09 '22 at 10:25
  • 1
    Show the ways that did not work. – mplungjan Nov 09 '22 at 10:26
  • Does this answer your question? [Most efficient method to groupby on an array of objects](https://stackoverflow.com/questions/14446511/most-efficient-method-to-groupby-on-an-array-of-objects) – pilchard Nov 09 '22 at 10:29
  • also: [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) or for two properties: [Group objects by multiple properties in array then sum up their values](https://stackoverflow.com/questions/46794232/group-objects-by-multiple-properties-in-array-then-sum-up-their-values) – pilchard Nov 09 '22 at 10:29

2 Answers2

0
const data = [
  {
    id: 'abc',
    data1: 3,
    data2: 'test1',
  },
  {
    id: 'abc',
    data1: 4,
    data2: 'test1',
  },
  {
    id: 'xyz',
    data1: 2,
    data2: 'test2',
  },
];

/**
 * To merge the array of objects by a key
 *
 * @param {any[]} objectArray The input object array
 * @param {string} mergeBy The key for merging the objects
 * @param {string} property The key to which the values should be aggregated
 * @returns
 */
const groupBy = (objectArray, mergeBy, property) =>
  Object.values(
    objectArray.reduce(
      (
        /** @type {{ [x: string]: {}; }} */ acc,
        /** @type {{ [x: string]: any; }} */ obj
      ) => {
        const key = obj[mergeBy];
        const curGroup = acc[key] ?? {};
        // if the key is already exists in the accumulator object
        if (curGroup?.[property] && obj?.[property]) {
          curGroup[property].push(obj[property]);
          return { ...acc, [key]: { ...curGroup } };
        }
        if (!curGroup?.[property] && obj?.[property]) {
          const data = { ...obj } ;
          data[property] = [];
          data[property].push(obj[property]);
          return { ...acc, [key]: { ...data } };
        }
      },
      {}
    )
  );

groupBy(data, 'id', 'data1');
samith
  • 1,042
  • 8
  • 5
0

Thanks all for your help. I have solved using the following logic

let helper = {}
let result = labels.reduce(function(r, o) {
  var key = o.id
  var outputpDic = {
    'id': o.id,
    'data1': [o.data1],
    'data2': o.data2,
  }

  if (!helper[key]) {
    helper[key] = Object.assign({}, outputpDic)
    r.push(helper[key])
  } else {
    helper[key].data1.push(o.data1)
  }

  return r
}, [])
Mustafa Poya
  • 2,615
  • 5
  • 22
  • 36
br0ek
  • 21
  • 4