Skip to content

A simple way to filter an array of objects with multiple filters

License

Notifications You must be signed in to change notification settings

xdigu/multiple-filter-array

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NPM Version NPM Downloads NPM License

A simple way to filter an array with multiple filters

To install :

$ npm i multiple-array-filter

or

$ yarn add multiple-array-filter

Usage:

This library was made to help you to filter an array with a lot of filters, imagine that you have an app with a flatlist that contains a list of user, so to each user there is a lot of fieldes to search to.

You can make a single filter field and than search into each user's field with this library. Another approach is creating dinamics filters where each filter is a field of your user and than you pass this to the library.

'use strict'

const { filterAndSortArray } = require('multiple-array-filter');

const items = [
  { name: 'Steve Jobs', id: 1 },
  { name: 'Elon Musk', id: 2 },
  { name: 'Robert C. Martin', id: 3 },
  { name: 'Bill Gates', id: 4 },
]

const filters = [
  { field: 'name', search: ['bil', 'job'] },
  { field: 'id', search: 3 },
]

const filtered_array = filterAndSortArray(items, filters, 'id');

console.log(filtered_array);
/**
 * [
 *  { name: 'Steve Jobs', id: 1 },
 *  { name: 'Robert C. Martin', id: 3 },
 *  { name: 'Bill Gates', id: 4 }
 * ]
 */

About

A simple way to filter an array of objects with multiple filters

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published