charlieanstey
7/2/2017 - 10:07 PM

Symmetric diff of 2 JavaScript object arrays. Written in ES6 and checks against a numeric 'id' property in each object. Returns an object of

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

_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))
    }
}