Reputation: 1430
Here is a working example (and here is a link to something you can play with):
var idDateArray = [
{
id: '1',
updateTime: '00:00:00.000Z',
updateDate: '2019-01-01'
},
{
id: '2',
updateTime: '00:00:00.000Z',
updateDate: '2021-01-01'
},
{
id: '3',
updateTime: '00:00:00.000Z',
updateDate: '2019-01-01'
},
{
id: '4',
updateTime: '00:00:00.000Z',
updateDate: '2019-01-01'
},
{
id: '5',
updateTime: '00:00:00.000Z',
updateDate: '2020-01-01'
}
]
var itemArray = [
{
updateDate: '2020-01-01',
updateTime: '00:00:00.000Z',
id: '4'
},
{
updateDate: '2020-01-02',
updateTime: '00:00:00.000Z',
id: '2'
},
{
updateDate: '2021-01-02',
updateTime: '00:00:00.000Z',
id: '3'
},
{
updateDate: '2020-01-01',
updateTime: '00:00:00.000Z',
id: '1'
}
]
var filteredArray = idDateArray.filter((row) =>
itemArray.some(
(item) =>
//item.id !== row.id || // <-LINE IN QUESTION
(item.id === row.id &&
Date.parse(row.updateDate + 'T' + row.updateTime) >
Date.parse(item.updateDate + 'T' + item.updateTime)),
),
);
console.log(filteredArray);
What I am not understanding is why this ||
condition won't work as I expect? In the above example, when the LINE IN QUESTION is removed, all objects in idDateArray
whose id
matches those in itemArray
AND date/time is >
those in itemArray
should be removed. And they are (id 2 is all that is left).
I then add in the or condition, which I would then expect to not only filter on matched id's and greater than date time, but also filter those that dont have any match on id. I.E. If either condition is true, remove from idDateArray
.
Upvotes: 1
Views: 44
Reputation: 64695
I mean, you're saying "keep it if there is any item in the array where item.id !== row.id
", and there's presumably always going to be at least a single item where the id doesn't match, you're using .some
not .every
Upvotes: 2