Created
July 2, 2017 22:07
-
-
Save charlieanstey/aa30f9f839c0c37d6271ad993ac46a14 to your computer and use it in GitHub Desktop.
Symmetric diff of 2 JavaScript object arrays. Written in ES6 and checks against a numeric 'id' property in each object. Returns an object of added and removed arrays
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_diffById(newArr, oldArr) { | |
function notContainedIn(arr) { | |
return function arrNotContains(element) { | |
return arr.map(el => { return el.id }).indexOf(element.id) === -1 | |
} | |
} | |
return { | |
added: newArr.filter(notContainedIn(oldArr)), | |
removed: oldArr.filter(notContainedIn(newArr)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment