filter-object-array

0.3.6 • Public • Published

filter-object-array

npm version npm downloads Coveralls Status

NPM

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' } ] */

Tests

Package Sidebar

Install

npm i filter-object-array

Weekly Downloads

5

Version

0.3.6

License

MIT

Unpacked Size

7.79 kB

Total Files

6

Last publish

Collaborators

  • circa10a