Git Product home page Git Product logo

diff-arrays-of-objects's Introduction

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

diff-arrays-of-objects's People

Contributors

baixiaoji avatar dependabot[bot] avatar gainxglobal avatar malcolmvr avatar xarsh avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.