What is array-filter?
The array-filter npm package provides a simple utility for filtering elements in an array based on a provided callback function. It is a lightweight and straightforward implementation of the Array.prototype.filter method, making it useful for environments where the native method is not available or for those who prefer a minimalistic approach.
What are array-filter's main functionalities?
Basic Filtering
This feature allows you to filter an array of numbers to only include even numbers. The callback function checks if each number is even.
const filter = require('array-filter');
const numbers = [1, 2, 3, 4, 5];
const evenNumbers = filter(numbers, function(number) {
return number % 2 === 0;
});
console.log(evenNumbers); // [2, 4]
Filtering with Custom Conditions
This feature demonstrates filtering an array of strings to only include words longer than 5 characters. The callback function checks the length of each word.
const filter = require('array-filter');
const words = ['apple', 'banana', 'cherry', 'date'];
const longWords = filter(words, function(word) {
return word.length > 5;
});
console.log(longWords); // ['banana', 'cherry']
Filtering Objects in an Array
This feature shows how to filter an array of objects to only include those where the age property is 30 or above. The callback function checks the age property of each object.
const filter = require('array-filter');
const people = [
{ name: 'Alice', age: 25 },
{ name: 'Bob', age: 30 },
{ name: 'Charlie', age: 35 }
];
const adults = filter(people, function(person) {
return person.age >= 30;
});
console.log(adults); // [{ name: 'Bob', age: 30 }, { name: 'Charlie', age: 35 }]
Other packages similar to array-filter
lodash
Lodash is a modern JavaScript utility library delivering modularity, performance, and extras. It provides a wide range of utility functions, including array filtering with the _.filter method. Compared to array-filter, Lodash offers a more comprehensive set of utilities beyond just filtering.
underscore
Underscore is a JavaScript library that provides a whole mess of useful functional programming helpers without extending any built-in objects. It includes a filter function similar to array-filter but also offers many other utilities for working with arrays, objects, and functions.
ramda
Ramda is a practical functional library for JavaScript programmers. It makes it easy to create functional pipelines and includes a filter function. Ramda emphasizes a functional programming approach, which can be more powerful and flexible compared to the simpler array-filter.
array-filter
Array#filter
for older browsers.
Usage
var filter = require('array-filter');
var array = [1, 2, 3];
console.log(filter(array, function (el, i, arr) {
return false;
}));
Installation
With npm do
$ npm install array-filter
Then bundle for the browser with browserify.
License
(MIT)
Copyright (c) 2013 Julian Gruber <julian@juliangruber.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.