1.1.9 • Published 4 years ago
diff-arrays-of-objects v1.1.9
diff-arrays-of-objects
Compare two arrays of objects, finding added, removed, updated and identical objects. Details the differences between updated objects.
Install
$ npm install diff-arrays-of-objects --save
Usage
const diff = require('diff-arrays-of-objects');
var result = diff(
[
{id: 1, name: 'a'},
{id: 2, name: 'b'},
{id: 3, name: 'c'},
{id: 4, name: 'd'},
{id: 5, name: 'e'}
],
[
{id: 1, name: 'a'},
{id: 2, name: 'z'},
{id: 7, name: 'e'}
],
'id'
);
console.log(result)
// {
// added: [
// { id: 7, name: 'e' }
// ],
// removed: [
// { id: 3, name: 'c' },
// { id: 4, name: 'd' },
// { id: 5, name: 'e' }
// ],
// updated: [
// { id: 2, name: 'z' }
// ],
// same: [
// { id: 1, name: 'a' }
// ]
// }
API
diff-arrays-of-objects (first, second, idField, options)
first
Required
Type: array
First array to be compared.
second
Required
Type: array
Second array to be compared.
idField
Required
Type: string
The id field that is used to compare the arrays. Defaults to 'id'.
options
Type: object
{
compareFunction: <Func> // defaults to lodash's isEqual; must accept two parameters (o1, o2)
updatedValues: <Number> // controls what gets returned in the "updated" results array:
// diff.updatedValues.first (1): the value from the first array
// diff.updatedValues.second (2): the value from the second array (default)
// diff.updatedValues.both (3): both values, as an array [first, second]
// diff.updatedValues.bothWithDeepDiff (4): both values, plus the results of the deep-diff module; [first, second, deep-diff]
}
Examples:
const diff = require('diff-arrays-of-objects');
const first = [{ id: 1, letter: 'a' }];
const second = [{ id: 1, letter: 'b' }];
const result = diff (first, second, idField, { updatedValues: diff.updatedValues.first });
// result.updated is [{ id: 1, letter: 'a' }]
const result = diff (first, second, idField, { updatedValues: diff.updatedValues.second });
// result.updated is [{ id: 1, letter: 'b' }]
const result = diff (first, second, idField, { updatedValues: diff.updatedValues.both });
// result.updated is [{ id: 1, letter: 'a' }, { id: 1, letter: 'b' }]
const result = diff (first, second, idField, { updatedValues: diff.updatedValues.bothWithDeepDiff });
// result.updated is [{ id: 1, letter: 'a' }, { id: 1, letter: 'b' }, { kind: 'E', path: ['letter'], lhs: 'a', rhs: 'b' }]
See deep-diff for more info on deep-diff results
License
MIT