filter-object-array
v0.3.6
Published
Filter an array of objects with an object
Downloads
17
Readme
filter-object-array
A small library to make filtering array of objects easier
Installation
npm install filter-object-array
Usage
const filterObjectArray = require('filter-object-array');
const arr = [
{
car: 'toyota',
color: 'blue',
year: 2010,
trans: 'auto',
warrantyEnd: '2013',
},
{
car: 'toyota',
condition: 'good',
color: 'green',
year: 2010,
trans: 'manual',
warrantyEnd: '2013',
},
{
car: 'ford',
color: 'yellow',
year: 2012,
trans: 'auto',
warrantyEnd: '2015',
},
];
// Filter using same data types
const sameDataType = async () => {
const filtersMatchType = {
color: 'blue',
year: 2010,
};
console.log(await filterObjectArray({ array: arr, objFilter: filtersMatchType }));
};
// Filter array using different data types
const diffDataType = async () => {
const filtersDiffType = {
warrantyEnd: 2015,
};
console.log(await filterObjectArray({ array: arr, objFilter: filtersDiffType, ignoreDataType: true }));
};
sameDataType();
/* [ { car: 'toyota',
color: 'blue',
year: 2010,
trans: 'auto',
warrantyEnd: '2013' } ] */
diffDataType();
/* [ { car: 'ford',
color: 'yellow',
year: '2012',
trans: 'auto',
warrantyEnd: '2015' } ] */