Let's imagine I have an array of objects structured like the following:
[
{value: "2021", label: "REDS"},
{value: "2020", label: "REDS"},
{value: "2021", label: "COPASA"},
{value: "2021", label: "CEMIG_CLIENTES"},
{value: "2016", label: "CEMIG_CLIENTES"},
{value: "2016", label: "RFQSA"}
]
The task at hand is to identify duplicate entries based on the value property and create a new array that combines their label properties. The desired output should look like this:
[
{value: "2021", label: "REDS/COPASA/CEMIG_CLIENTES"},
{value: "2020", label: "REDS"},
{value: "2016", label: "CEMIG_CLIENTES/RFQSA"}
]
How can I go about achieving this? I'm currently stuck in my attempts. I've tried extracting unique values using the following code snippet:
const unique = arr.map(e => e['value'])
.map((e, i, final) => final.indexOf(e) === i && i)
.filter(obj=> aux2[obj])
.map(e => aux2[e]);
However, this approach falls short as it overlooks the preservation of labels. Another idea I had was to sort the array and devise a method to pinpoint duplicate positions so they can be merged accordingly. But so far, I haven't been successful in implementing it. Is there perhaps a more effective solution out there? If not, could someone offer guidance on how to tackle this challenge?
Thank you for your help in advance!