npm.io
1.0.0 • Published 4 years ago

smb-js-array

Licence
ISC
Version
1.0.0
Deps
0
Size
2 kB
Vulns
0
Weekly
0

mb-js-array

This package will help you to get the difference between two arrays of objects in JavaScript.

Installation

npm i mb-js-array

Get the difference between two arrays of objects

the function takes three arguments
Parameter Description
param1 Required. the first array
param2 Required. the second array
param3 Required. the key to compare
Example
const diffArray = require('mb-js-array')

const arrayOne = [ 
    { id: 1, firstName: "Mahmoud", lastName:"Barry" },
    { id: 2, firstName: "Ben" ,lastName:"Barry" },
    { id: 3, firstName: "Demba" ,lastName:"Barry" },
  ];
  
  const arrayTwo = [
    { id: 1, firstName: "Mahmoud",lastName:"Barry" },
    { id: 2, firstName: "Sultan",lastName:"Ndiaye" },

  ];

// compare by id 
const byId = diffArray(arrayOne, arrayTwo,key="id")
console.log(byId) // [ { id: 3, firstName: 'Demba', lastName: 'Barry' } ]

// compare by lastName
const bylastName = diffArray(arrayOne, arrayTwo,key="lastName")
console.log(bylastName) // [ { id: 2, firstName: 'Sultan', lastName: 'Ndiaye' } ]

/* if no item the function will return */`no item`

Keywords