Created
August 13, 2014 22:46
-
-
Save aalvesjr/0214e2bd3eac714f4d69 to your computer and use it in GitHub Desktop.
map e reduce em JS
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 products = [ | |
{name: "Product A"} | |
,{name: "Product B", price: 100} | |
,{name: "Product C", price: 200} | |
,{name: "Product D", offerPrice: 100, price: 150} | |
]; | |
function filterPrice(product){ | |
var price = product["offerPrice"] ? product["offerPrice"] : product["price"]; | |
return price ? price : 0; | |
}; | |
function sum(a, b) { | |
return a + b; | |
}; | |
var totalSales = products.map(filterPrice).reduce(sum,0); | |
// 400 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment