Skip to content
This repository was archived by the owner on Sep 14, 2024. It is now read-only.

circa10a/filter-object-array

Repository files navigation

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

About

Small Node.js library to filter an array of objects with an object

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •