Consider these two lists:
let oldItems = [ 1, 2, 3, 4 ];
let newItems = [0, 1, 2, 5];
Moving from oldItems
to newItems
, you see that:
Here is the code that uses filter and some to calculate the inserts and deletes in Javascript:
let inserts = newItems.filter(x => !oldItems.some(y => x === y));
let deletes = oldItems.filter(x => !newItems.some(y => x === y));
console.log(inserts);
console.log(deletes);
result:
[ 0, 5 ]
[ 3, 4 ]
If you want to create inserts and deletes for lists that contain more complex objects like class instances, you might want to consider replacing ===
with an equals function