Skip to content

Instantly share code, notes, and snippets.

@davodey
Created January 24, 2017 22:26
Show Gist options
  • Save davodey/09433c05fdffeea6176563e36d6ded58 to your computer and use it in GitHub Desktop.
Save davodey/09433c05fdffeea6176563e36d6ded58 to your computer and use it in GitHub Desktop.
Example using the ES6 filter helper.
var products = [
{ name: 'cucumber', type: 'vegetable'},
{ name: 'banana', type: 'fruit'},
{ name: 'orange', type: 'fruit'}
];
var filteredProducts = [];
// what we want to get away from
for (var i = 0; i < products.length; i ++) {
if (products[i].type === 'fruit') {
filteredProducts.push(products[i]);
}
}
// returns a true or false statement
// if true the result will be placed in new array
var filtered = products.filter(function(product){
return product.type === 'fruit'
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment