I'm facing a challenge with filtering an array of objects based on the condition that myDatas.items.flaggedItem
should not be null
, while also eliminating duplicates where myDatas.items.id
are identical. This scenario should result in only 2 items being returned.
"myDatas":[
{
"id": "id1",
"items":[
{
"id": "idOne",
"flaggedItem": null,
},
{
"id": "idTwo",
"flaggedItem": 1,
},
},
{
"id": "id2",
"items":[
{
"id": "idTwo",
"flaggedItem": 1,
},
{
"id": "idOne",
"flaggedItem": 2,
},
}
]
Although I've managed to successfully filter items based on myDatas.items.flaggedItem
not being null
, I can't seem to figure out how to remove the duplicates effectively.
let test1 = myDatas
?.map(myData => {
let test2 = myData.items?.filter(
items => items.flaggedItem || items.flaggedItem === 0
);
return { ...myData, items: test2 };
});