I am working with a data structure retrieved from an API, and it looks like this:
[
{
id: '10000844',
text_id: '10000844-01',
},
{
id: '10000844',
text_id: '10000844-02',
},
{
id: '12000844',
text_id: '12000844-03',
},
{
id: '12000844',
text_id: '12000844-07',
},
{
id: '12000814',
text_id: '12000844-07',
},
{
id: '12002812',
text_id: '12000844-07',
},
{
id: '12000814',
text_id: '12000844-08',
},
]
After cleaning up the code, I want the result to only contain the first occurrence of each unique ID:
[
{
id: '10000844',
text_id: '10000844-01',
},
{
id: '12000844',
text_id: '12000844-03',
},
{
id: '12000814',
text_id: '12000844-07',
},
{
id: '12002812',
text_id: '12000844-07',
},
]
However, my current code is only returning the last duplicate found in a unique array. Here is the snippet:
let uniqueArray = [...new Map(data.map(item =>
[item.id, item])).values()];