Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including array-compare with all npm packages installed. Try it out:

var arrayCompare = require("array-compare")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

array-compare v2.0.0

shallow array diff by object equality or id

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

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free