JavaScript - how to filter array

0 points
Created by:
Dirask Community

In this article, we would like to show you how to filter an array in JavaScript.

Practical example

Below we have food array with some records. In this example, we filter the array using filter() method to get only food with fruit type.

// ONLINE-RUNNER:browser;

const food = [
	{name: 'Apple', type: 'fruit'},
	{name: 'Broccoli', type: 'vegetable'},
 	{name: 'Banana', type: 'fruit'},
 	{name: 'Fries', type: 'fast-food'},
 	{name: 'Cherry', type: 'fruit'}

const fruits = food.filter((item) => item.type === 'fruit');
console.log(JSON.stringify(fruits, null, 4));


Native Advertising
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂