Skip to content

Instantly share code, notes, and snippets.

@aalvesjr
Created August 13, 2014 22:46
Show Gist options
  • Save aalvesjr/0214e2bd3eac714f4d69 to your computer and use it in GitHub Desktop.
Save aalvesjr/0214e2bd3eac714f4d69 to your computer and use it in GitHub Desktop.
map e reduce em JS
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