Skip to content

Instantly share code, notes, and snippets.

@GSoster
Last active September 22, 2017 13:29
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 GSoster/98aff5596f6c0c1c93df7dbfaf6aedc9 to your computer and use it in GitHub Desktop.
Save GSoster/98aff5596f6c0c1c93df7dbfaf6aedc9 to your computer and use it in GitHub Desktop.
Array.reduce in Javascript - blog post
[].reduce(function(a, b){
return a + b;
});
[].reduce(function(a, b){
return a + b;
});
var arr = [7, 4, 9, 2, 11];
var min = arr.reduce(function(a, b){
return Math.min(a, b);
});
reduce (callback, [valorInicial])
var carrinho = [
{"_id": "xht1", "item" : "Adaptador", "value": 3.5},
{"_id": "cf12", "item" : "Cabo USB", "value": 11.2},
{"_id": "ldi8", "item" : "Lâmpada LED", "value": 17.00}
];
var total = carrinho.reduce(function(acumulador, item){
return acumulador + item.value;
}, 0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment