I have original data like this in array arr
[
{Id:1,
Date:20/06/2023,
ProdId:1
},
{Id:2,
Date:21/06/2023,
ProdId:1
},
{Id:3,
Date:21/06/2023,
ProdId:2
},
{Id:4,
Date:23/06/2023,
ProdId:2
},
{Id:5,
Date:20/06/2023,
ProdId:3
}
]
I want the items with unique prodIds and it should be latest. I should get the below data
[
{Id:2,
Date:21/06/2023,
ProdId:1
},
{Id:4,
Date:23/06/2023,
ProdId:2
},
{Id:5,
Date:20/06/2023,
ProdId:3
}
]
What I need to do? Thanks in advance!!
2
Answers
I’m sure there is probably an easier way, but this worked for me:
This code first sorts by the date. Then filters for unique values based on the
ProdId
.Trick: