Created
April 26, 2021 14:31
-
-
Save pawiromitchel/605a17464c5b938e919ac9e4215e11fe to your computer and use it in GitHub Desktop.
Compare Array with JSON objects to get removed and added objects
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
var array1 = [{id: 1}, {id: 2}, {id: 3}]; | |
var array2 = [{id: 2}, {id: 4}]; | |
var array1Converted = []; | |
var array2Converted = []; | |
// array met JSON objects omzetten tot normale arrays | |
array1.map(el => array1Converted.push(el.id)); | |
array2.map(el => array2Converted.push(el.id)); | |
// db array vergelijken met de frontend array om de verwijderde ids te krijgen | |
var removedID = array1Converted.filter(x => array2Converted.indexOf(x) === -1); | |
// frontend array vergelijken met db array om de toegevoegde ids te krijgen | |
var addedID = array2Converted.filter(x => array1Converted.indexOf(x) === -1); | |
console.log(removedID); | |
console.log(addedID); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment