Skip to content

compare 2 array of objects and filter out create/update/delete object

Notifications You must be signed in to change notification settings

thunderrun/compare-CUD

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

compare-CUD

Compare 2 array of objects and filter out create/update/delete objects

Usage

npm i 'compare-cud'
import compare from './index.js';

const newArray = [
  {
    id: 1,
    a: 1,
    b: 2
  },
  {
    id: 2,
    a: 1,
    b: 2,
    c: 3,
  },
  {
    id: 4,
    a: 0,
  },
  {
    id: 5,
    a: 1,
  },
];

const oldArray = [
  {
    id: 1,
    a: 1,
    b: 2
  },
  {
    id: 2,
    b: 2,
    c: 3,
  },
  {
    id: 3,
    d: 1,
    b: 2,
  },
  {
    id: 6,
    d: 1,
  },
];

const result = compare(newArray, oldArray);

/* result:
{
  "create": [
      {
          "id": 4,
          "a": 0
      },
      {
          "id": 5,
          "a": 1
      }
  ],
  "update": [
      {
          "id": 2,
          "a": 1,
          "b": 2,
          "c": 3
      }
  ],
  "delete": [
      {
          "id": 3,
          "d": 1,
          "b": 2
      },
      {
          "id": 6,
          "d": 1
      }
  ]
}

*/

About

compare 2 array of objects and filter out create/update/delete object

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published