This content originally appeared on CodeSource.io and was authored by Ariessa Norramli
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.
The post How to Filter A Collection Using Lodash appeared first on CodeSource.io.
This content originally appeared on CodeSource.io and was authored by Ariessa Norramli
Ariessa Norramli | Sciencx (2021-02-20T12:07:39+00:00) How to Filter A Collection Using Lodash. Retrieved from https://www.scien.cx/2021/02/20/how-to-filter-a-collection-using-lodash/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.