Last active
September 22, 2017 13:29
-
-
Save GSoster/98aff5596f6c0c1c93df7dbfaf6aedc9 to your computer and use it in GitHub Desktop.
Array.reduce in Javascript - blog post
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
[].reduce(function(a, b){ | |
return a + b; | |
}); |
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
[].reduce(function(a, b){ | |
return a + b; | |
}); |
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
var arr = [7, 4, 9, 2, 11]; | |
var min = arr.reduce(function(a, b){ | |
return Math.min(a, b); | |
}); |
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
reduce (callback, [valorInicial]) |
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
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