Below is an array that I have:
var somevalue = [{
code: 1,
name: 'a1'
}, {
code: 2,
name: 'b1'
}, {
code: 1,
name: 'a2'
},
{
code: 1,
name: 'a3'
},
{
code: 2,
name: 'b2'
}
]
I am looking to identify duplicate elements based on the code
, and then merge all elements with the same code into one. The desired output would be as follows:
var somevalue = [{
code: 1,
name: 'a1, a2'
}, {
code: 2,
name: 'b1, b2, b3'
}
]
Is it possible to achieve this using underscoreJS
?
I currently can do this using a for-loop
. However, in real-life scenarios, the array is quite large and contains JSON objects with multiple properties. Therefore, I am in search of a performance-oriented solution.