1.0.0 • Published 2 years ago
smb-js-array v1.0.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`
1.0.0
2 years ago