I am working with an array of JavaScript objects that have specific properties: _id, isVersionFrom, createdAt
. The _id
and isVersionFrom
properties store MongoDB _ids (with isVersionFrom
being false for the original object), while createdAt
stores a timestamp from epoch. My goal is to extract an array containing only the latest version for each _id
.
For example, consider the following array:
[
{
_id: 'CXsqvJ3ArwpM7aQwg',
createdAt: 1493630867411,
isVersionFrom: false
},
{
_id: 'nnFthaqJ254BQozSp',
createdAt: 1493630967411,
isVersionFrom: 'CXsqvJ3ArwpM7aQwg'
},
{
_id: 'Jksfe6Aaof9hT8CW9',
createdAt: 1493631067411,
isVersionFrom: 'CXsqvJ3ArwpM7aQwg'
},
{
_id: 'HSitgQdFwYHqCjRax',
createdAt: 1493631067411,
isVersionFrom: false
},
{
_id: 'vhZZCKhyQgkNSGjqK',
createdAt: 1493631077411,
isVersionFrom: 'HSitgQdFwYHqCjRax'
},
]
The expected result should be:
[
{
_id: 'Jksfe6Aaof9hT8CW9',
createdAt: 1493631067411,
isVersionFrom: 'CXsqvJ3ArwpM7aQwg'
},
{
_id: 'vhZZCKhyQgkNSGjqK',
createdAt: 1493631077411,
isVersionFrom: 'HSitgQdFwYHqCjRax'
},
]
This entails filtering out all older versions of each ID.