Lodash's `filter()` Function

Lodash’s `filter()` Function

Lodash’s `filter()` Function

In this post, we will give you information about Lodash’s `filter()` Function. Here we will give you detail about Lodash’s `filter()` Function And how to use it also give you a demo for it if it is necessary.

Given an array arr, Lodash’s filter() function returns an array containing all the elements in arrĀ for which the function returned a truthy value.

const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];

_.filter(arr, isEven); // [2, 4, 6, 8]
function isEven(v) { return v % 2 === 0; }

The function you pass to filter() is called the predicate. If the predicate returns a falsy value (like null, undefined, , or ''), Lodash filters that value out.

const arr = [null, false, 0, 'hello'];

_.filter(arr, v => v); // ['hello']

On Arrays of Objects for Lodash’s `filter()` Function

The filter() function has a couple convenient shorthands for dealing with arrays of objects. If you pass a string predicate
instead of a function, Lodash will filter by whether that property is truthy or falsy.

const arr = [
  { hello: null },
  { hello: false },
  { hello: 0 },
  { hello: 'world' }

_.filter(arr, 'hello'); // [{ hello: 'world' }]

If your predicate is an object obj, Lodash will filter objects that match the given predicate. In other words, Lodash will match objects that have the same value as obj for all properties in obj.

const arr = [
  { firstName: 'Will', lastName: 'Riker', rank: 'Commander' },
  { firstName: 'Beverly', lastName: 'Crusher', rank: 'Commander' },
  { firstName: 'Wesley', lastName: 'Crusher', rank: 'Ensign' }

// ['Riker', 'Crusher']
_.filter(arr, { rank: 'Commander' }).map(v => v.lastName);

// ['Beverly', 'Wesley']
_.filter(arr, { lastName: 'Crusher' }).map(v => v.firstName);

// ['Beverly']
_.filter(arr, { lastName: 'Crusher', rank: 'Commander' }).map(v => v.firstName);

On Objects

The _.filter() function can also accept an object as a parameter, rather than an array. Calling _.filter(obj, fn) behaves similarly
to _.filter(Object.values(obj), fn).

const obj = {
  one: 1,
  two: 2,
  three: 3,
  four: 4
_.filter(obj, v => v % 2 === 0); // [2, 4]


Lodash is a JavaScript library which provides utility functions for common programming tasks using the functional programming paradigm. Lodash is a JavaScript library that helps programmers write more concise and maintainable JavaScript. It can be broken down into several main areas: Utilities: for simplifying common programming tasks such as determining type as well as simplifying math operations.

Why Lodash?

Lodash makes JavaScript easier by taking the hassle out of working with arrays, numbers, objects, strings, etc.Lodash’s modular methods are great for:

  • Iterating arrays, objects, & strings
  • Manipulating & testing values
  • Creating composite functions

Hope this code and post will helped you for implement Lodash’s `filter()` Function – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US