Created
March 11, 2019 18:56
-
-
Save RedGhoul/51948a1faa3cabb5b3dee37fd26cd9c8 to your computer and use it in GitHub Desktop.
FilterInJS
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 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