Created
January 1, 2017 19:36
-
-
Save mannuelf/083fc747488259616c7066bfdbeec97e to your computer and use it in GitHub Desktop.
filtering on an array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const products = [ | |
{ name: 'cucumber', type: 'vegetable' }, | |
{ name: 'banana', type: 'fruit' }, | |
{ name: 'celery', type: 'vegatable' }, | |
{ name: 'orange', type: 'fruit' }, | |
]; | |
let filteredProducts = []; | |
/* | |
for (var i = 0; i < products.length; i++) { | |
if (products[i].type === 'fruit') { | |
filteredProducts.push(products[i]); | |
} | |
} | |
*/ | |
products.filter(function(product) { | |
return product.type === 'fruit'; | |
}); | |
filteredProducts; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment