Created
February 19, 2019 04:41
-
-
Save rebelnz/dee532e1c8e5a3c3f0d1951f5ec10519 to your computer and use it in GitHub Desktop.
filter, map, reduce javascript
This file contains hidden or 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
var pets = [ | |
{name:"rover",animal:"dog",amount:27}, | |
{name:"freddy",animal:"turtle",amount:27}, | |
{name:"deefor",animal:"dog",amount:27}, | |
{name:"sir barkalot",animal:"dog",amount:27}, | |
{name:"cedric",animal:"snake",amount:27} | |
] | |
// var dogs = pets.filter(function(pets) { | |
// return pets.animal == "dog" | |
// }) | |
const dogs = pets.filter((e) => e.animal === "dog") | |
// var names = pets.map(function(p) { return p.name}); | |
const names = pets.map((p) => p.name); | |
// var total = pets.reduce(function(sum,pet) { | |
// console.log("inside reduce : ", sum, pet) | |
// return sum + pet.amount | |
// },0) | |
const total = pets.reduce((sum ,pet) => sum + pet.amount, 0) | |
console.log(total); | |
console.log(dogs); | |
console.log(names); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment