Here is an array of objects with mock timestamps:
var firstArr = [{
id: 1,
a: 2,
timestemp: 111
}, {
id: 2,
a: 4,
timestemp: 222
}, {
id: 3,
a: 6,
timestemp: 333
}, {
id: 1,
a: 3,
timestemp: 777
}, {
id: 3,
a: 5555,
timestemp: 5555
}];
I want to filter this array and create a new array with unique values.
The final array I'm looking for is:
var endArr = [{
id: 1,
a: 3,
timestemp: 777
}, {
id: 2,
a: 4,
timestemp: 222
}, {
id: 3,
a: 5555,
timestemp: 555
}];
To achieve this, I need to filter the array by two criteria:
- Unique ID (only entries with IDs 1 & 3 appear once)
- Timestamp (include only objects with the latest timestamp)
Is it possible to accomplish this using array methods like map/reduce/filter?
I attempted to use array.filter but was unsuccessful.