flitbit / diff

Javascript utility for calculating deep difference, capturing changes, and applying changes across objects; for nodejs and the browser.
MIT License
2.99k stars 213 forks source link

get list of changed items? #68

Closed FezVrasta closed 5 years ago

FezVrasta commented 8 years ago

Hi,

I need to get just a simple list of the items that have been changed or added in an array, without any other information.

A = [ {a: 1}, {a: 2} ]
B = [ {a: 3}, {a: 2} ]

=> [ {a: 3} ]

How can I achieve it?

sh977218 commented 5 years ago

you can use lodash difference achieve it. https://lodash.com/docs/4.17.10#difference

Berkmann18 commented 5 years ago

^ Or you could use this:

const diff = arr => arr.reduce((a, b) => a.filter(c => !b.includes(c)))

Taken from here:

let arrays = [[1, 2, 3, 4, 5], [5, 2, 10]];
console.log(arrays.reduce((a, b) => a.filter(c => !b.includes(c))));
// output: [1, 3, 4]