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. In…

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

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


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » How to Filter A Collection Using Lodash." Ariessa Norramli | Sciencx - Saturday February 20, 2021, https://www.scien.cx/2021/02/20/how-to-filter-a-collection-using-lodash/
HARVARD
Ariessa Norramli | Sciencx Saturday February 20, 2021 » How to Filter A Collection Using Lodash., viewed ,<https://www.scien.cx/2021/02/20/how-to-filter-a-collection-using-lodash/>
VANCOUVER
Ariessa Norramli | Sciencx - » How to Filter A Collection Using Lodash. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/02/20/how-to-filter-a-collection-using-lodash/
CHICAGO
" » How to Filter A Collection Using Lodash." Ariessa Norramli | Sciencx - Accessed . https://www.scien.cx/2021/02/20/how-to-filter-a-collection-using-lodash/
IEEE
" » How to Filter A Collection Using Lodash." Ariessa Norramli | Sciencx [Online]. Available: https://www.scien.cx/2021/02/20/how-to-filter-a-collection-using-lodash/. [Accessed: ]
rf:citation
» How to Filter A Collection Using Lodash | Ariessa Norramli | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.