Skip to content
Small Node.js library to filter an array of objects with an object
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
examples
test
.eslintignore
.eslintrc.json
.gitignore
.npmignore
.travis.yml
LICENSE
README.md
index.js
package-lock.json
package.json

README.md

filter-object-array

Travis npm version npm downloads Dependencies status Dev Dependencies status Known Vulnerabilities Coveralls Status

NPM

A small library to make filtering array of objects easier

Installation

npm install filter-object-array

Requirements

  • Node.js >=7.6.0 (async/await support)

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

You can’t perform that action at this time.