I have the following object:
a = [
{id: 1, comp: 'ans', stat: 'www', value: ['1', '2']},
{id: 2, comp: 'qsn', stat: 'xxx', value: ['a', 'b']},
{id: 3, comp: 'ans', stat: 'yyy', value: ['3', '4']},
{id: 4, comp: 'qsn', stat: 'zzz' ,value: ['c', 'd']}
]
I wanted the best possible way to combine the value array inside of the objects where the key comp matches and the other properties that do not match have the property of the first element intact and concat only the values array. The output I want to achieve in the above scenario is :
[
{id: 1, comp: 'ans', stat: 'www', value: ['1', '2', '3', '4']},
{id: 2, comp: 'qsn', stat: 'xxx', value: ['a', 'b', 'c', 'd']}
]
group.reduce((o1, o2) => ({ ...o1, value: o1.value.concat(o2.value) }))