I'm trying to filter the array:
const arr = [
{
id: '1',
modifiedTime: '2022-08-11T12:30:36.191Z',
modifiedUser: 'test'
},
{
id: '2',
modifiedTime: '2022-08-11T12:30:36.191Z',
modifiedUser: 'test'
},
{
id: '2',
modifiedTime: '2022-09-12T12:30:36.191Z',
modifiedUser: 'test'
},
{
id: '3',
modifiedTime: '2022-08-11T12:30:36.191Z',
modifiedUser: 'test'
},
];
to get results:
const arr = [
{
id: '1',
modifiedTime: '2022-08-11T12:30:36.191Z',
modifiedUser: 'test'
},
{
id: '2',
modifiedTime: '2022-09-12T12:30:36.191Z',
modifiedUser: 'test'
},
{
id: '3',
modifiedTime: '2022-08-11T12:30:36.191Z',
modifiedUser: 'test'
},
];
so, I need to filter the array in that way to stay with objects without duplicates, when an object has a duplicate, it should get only one with the largest modifiedTime.
I was trying to do this in that way:
arr.reduce((arr, el) => (((arr.find(({id}) => el.id === id)) || arr.push(el)), arr), [])
but in this way, it returns to me an array without objects with duplicates id but modifiedTime is not the largest.
Maybe someone will be able to suggest it to me?