1.0.0 • Published 2 years ago

smb-js-array v1.0.0

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

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
ParameterDescription
param1Required. the first array
param2Required. the second array
param3Required. 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`