Created
May 28, 2019 17:07
-
-
Save EdgarValfogo/e5bb436ed03c0d9ed5a134d2e2f733c1 to your computer and use it in GitHub Desktop.
Compare two arrays by its attributes
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
let arrA = [ | |
{ | |
nome: 'josias', | |
idade: 24 | |
}, | |
{ | |
nome: 'paulo', | |
idade: 25 | |
} | |
]; | |
let arrB = [ | |
{ | |
nome: 'josias', | |
idade: 28 | |
}, | |
{ | |
nome: 'paulo', | |
idade: 25 | |
} | |
];; | |
let diff = []; | |
arrA.forEach( ( el, index ) => { | |
let compareWith = arrB[index]; | |
let keys = Object.keys(el); | |
keys.forEach( ( k, i ) => { | |
if( compareWith[k] ) { | |
if( el[k] !== compareWith[k] ) { | |
diff.push( { current: el, updated: compareWith } ) | |
} | |
} | |
}); | |
}); | |
console.log( diff ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment