CodeSource code example

How to Filter A Collection Using Lodash


In this article, you will learn how to filter a collection using Lodash.

Let’s say you have a collection of food and their availability.

// Import Lodash library
import _ from "lodash";

var food = [
  { 'name': 'pizza', 'available': true },
  { 'name': 'meatball', 'available': false },
  { 'name': 'waffle', 'available': true },
];

In order to filter a collection using Lodash, you can use the _.filter method. In this example, you will filter the collection to only list out the food that is available.

// Import Lodash library
import _ from "lodash";

var food = [
  { 'name': 'pizza', 'available': true },
  { 'name': 'meatball', 'available': false },
  { 'name': 'waffle', 'available': true },
];
 
_.filter(food, ['available', false]); 
// => {available: false, name: "meatball"}

Note: The _.filter method functions by taking a collection and predicate as arguments, before returning a new filtered array.


Share on social media

//