2.0.0 • Published 7 years ago

array-compare v2.0.0

Weekly downloads
769
License
MIT
Repository
github
Last release
7 years ago

array-compare

Performs a shallow diff on a set of arrays returning found, missing and added pairs. It works by searching the second array for the items of the first comparing by object equality. Optionally, you can provided a third parameter which will be used to identify objects by their id. Array-compare is also UMD compatible.

NPM

build status npm npm

EXAMPLE

arrayCompare(arrayA, arrayB, <idName>)

var arrayCompare = require("array-compare")

var arrayA = [
  { sandwich: 'turkey', good: 'no' },
  { sandwich: 'blt', good: 'yes' }
];

var arrayB = [
  { sandwich: 'turkey', good: 'yes' },
  { sandwich: 'pbj', good: 'yes' }
];

// use either syntax
arrayCompare(arrayA, arrayB, 'sandwich');
arrayCompare({ a: arrayA, b: arrayB, id: 'sandwich' });

// returns
// {
//   found: [{
//     a: { sandwich: 'turkey', good: 'no' },
//     b: { sandwich: 'turkey', good: 'yes' },
//   }],
//   missing: [{
//     a: { sandwich: 'blt', good: 'yes' }
//     b: undefined
//   }],
//   added: [{
//     a: undefined
//     b: { sandwich: 'pbj', good: 'yes' }
//   }]
// }

NOTES

It's not nearly as useful for basic arrays filled with numbers or strings, but will work. Also, it doesn't currently provide any indication of a change in order, though it should be trivial to add as the code already has access to both a and b indexes thanks to Mout.js and its findIndex function.

LICENSE

MIT