Skip to content

Instantly share code, notes, and snippets.

@RedGhoul
Created March 11, 2019 18:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RedGhoul/51948a1faa3cabb5b3dee37fd26cd9c8 to your computer and use it in GitHub Desktop.
Save RedGhoul/51948a1faa3cabb5b3dee37fd26cd9c8 to your computer and use it in GitHub Desktop.
FilterInJS
const transActionsForMike = [ {Name: "Bike", Cost: 5000},
{Name: "Apple Music", Cost: 20},
{Name: "Cook Book", Cost: 30},
{Name: "Azure Hosting", Cost: 60},
{Name: "Phone Bill", Cost: 70}];
// As seen in Reduce and Map, we can add more elements into the
// anonymous functions such as the index and the whole array
// filter gives us each element in the array. We then have to return
// "true" to keep it in, or "false" to get rid of it.
// In the case below we are keeping the element in the new array we are returning
// if it's cost property is less than 100 or more than 20
let MikesSmallTransactions = transActionsForMike.filter((ele) => {
return ele.Cost < 100 && ele.Cost > 20;
});
console.log(MikesSmallTransactions);
// MikesSmallTransactions is now: [ { Name: 'Cook Book', Cost: 30 },
// { Name: 'Azure Hosting', Cost: 60 },
// { Name: 'Phone Bill', Cost: 70 } ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment