filtero
v2.0.0
Published
Filter an object by your own rules.
Readme
filtero
Fileter an object by your own rule (function) which received keys and values of the object as parameters. Returns a filtered new object.
Install
npm i filtero -SSyntax
filtero(obj, compareFunction [, thisArg])obj
Target object to be filtered
compareFunction
A function that defines the filtering rule. It will have key and value as parameters while iterating the object. If the function returns a truthy value. The current key/value pair will be reserved in the returning object, and vice versa.
thisArg [optional]
The value of this provided for the call to compareFunction.
Usage
import filtero from 'filtero'
const obj = {
a: 1,
b: 2,
c: 3,
d: 'D'
}
filtero(obj, (k, v) => k === 'a')
// { a: 1 }
filtero(obj, (k, v) => ['b', 'c'].indexOf(k) >= 0)
// { b: 2, c: 3 }
filtero(obj, (k, v) => typeof v === 'string')
// { d: 'D' }