I have a requirement where I have data similar to this:
[
{
"header": {
"httpFittingRequestAttempts": 1
},
"payload": {
"positionCode": "A10007",
"active": "A",
"classCode": "EX003",
"publishedTime": "2022-02-13 18:04:34.356251+00"
}
},
{
"header": {
"httpFittingRequestAttempts": 1
},
"payload": {
"positionCode": "E10003",
"active": "A",
"classCode": "EX002",
"publishedTime": "2022-02-21 03:34:10.139843+00"
}
},
{
"header": {
"httpFittingRequestAttempts": 1
},
"payload": {
"positionCode": "A10007",
"active": "A",
"classCode": "EX003",
"publishedTime": "2022-02-23 12:43:00.08635+00"
}
},
{
"header": {
"httpFittingRequestAttempts": 1
},
"payload": {
"positionCode": "A10007",
"active": "T",
"classCode": "EX004",
"publishedTime": "2022-02-24 12:00:00.08635+00"
}
}
]
I need to eliminate the duplicates for each positionCode
.
For example if there are multiple objects with the same positionCode
, I need to retain only the latest one in the array and remove all the other duplicates from the array and get back the array in the same format with the duplicates removed.
Please suggest a way to do this in Javascript.
I thought of grouping the objects using the positionCode
and then checking which is the latest. Not sure how to proceed and would appreciate help on this.