1.2.3 • Published 5 years ago
obj-functions v1.2.3
objFilter
Implementation of the array-like function filter() for regular javascript objects. With this tool it's possible to create a new object containing only a subset of the given object key-value pairs without using a for loop. It behaves in the same way of the native array filter function.
Getting Started
Installing
npm install obj-functionsUsage
Import objFilter function from the package:
import { objFilter } from "obj-functions";
const numbers = {
field1: 9,
field2: 5,
field3: -4
}
const negativeNumbers = objFilter(numbers, (field, value) => value < 0);
console.log(negativeNumbers); // will print { field3: -4 }objFilter(obj, callback) accepts 2 parameters:
objis the object to filtercallbackis the function used to filter the object and that loops through all object fields. It accepts 2 parameters, the generic object key and the relative value:callback(key, value). Callback should returntruewhenever current (key: value) pair should be kept in the returned object result.
Developing
Install all the dependencies:
npm installBuild it:
npm run buildTo run the tests:
npm testLicense
This project is licensed under the MIT License - see the LICENSE.md file for details