I have data from a form that needs to be compared to its older data.
I want to check if an object was in the previous old data and if its slot number changed.
Data example
const oldData = [{name: "John", slot: 2}, {name: "John", slot: 3}, {name: "John", slot: 4}]
const newData = [{name: "Alex", slot: 2}, {name: "John", slot: 3}, {name: "Bob", slot: 4}, {name: "John", slot: 5}]
// Duplications
const duplicateCreate = newData.filter((order1) => {
return oldData.some((order2) => {
return (
order1.name === order2.name &&
order1.slot !== order2.slot
);
});
});
console.log(duplicateCreate)
What I expect the output to be
[{name: "John", slot: 5}]
What I get
[{name: "John", slot: 3}, {name: "John", slot: 5}]
I have no Idea why the some function is passing john slot 2 as truthy, if I change my code to
order1.slot === order2.slot then the output will be
[{name: "John", slot: 3}]
So clearly its reading the && condition and clearly that logic works but the logic of checking for the object whose name is john and slot !== the same isnt working the way its written from my understanding so I am kind of at a loss right now.
3
Answers
I ran a couple of scenarios and this works fine:
Edit: Explanation
Original Code:
For each item in
newData
, it checks if there’s an item inoldData
with the same name but a different slot. This approach leads to false positives, like including{name: "John", slot: 3}
, since there’s another"John"
inoldData
with a different slot.Revised Code:
For each item in newData, it checks two things:
oldData
(just the name). The item fromnewData
is included in the result only if there’s a name match but not an exact match. This ensures that only true duplicates (same name, different slot, and no exact match inoldData
) are included in the result.Same as jagmitg, failed to convert to single check. But, it will be a bit more optimal in speed due to exiting the calculation on first .some:
There’s no need to check all conditions in jagmitg’s solution:
And a benchmark: