Skip to content

Instantly share code, notes, and snippets.

@EdgarValfogo
Created May 28, 2019 17:07
Show Gist options
  • Save EdgarValfogo/e5bb436ed03c0d9ed5a134d2e2f733c1 to your computer and use it in GitHub Desktop.
Save EdgarValfogo/e5bb436ed03c0d9ed5a134d2e2f733c1 to your computer and use it in GitHub Desktop.
Compare two arrays by its attributes
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