Created
February 25, 2020 16:15
-
-
Save brenofreire/5e73000606af227801b87a036b669886 to your computer and use it in GitHub Desktop.
Entendendo melhor o "reduce" de array do JavaScript
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
// Função que a recorrência de caracteres em uma frase. | |
function countChars(string) { | |
return string.split('').reduce((init, current) => { | |
return init[current] = (init[current] || 0) + 1; | |
}, {}); | |
} | |
// Esse segundo parâmetro do reduce é um valor inicial. | |
// Por ele iniciar com {} isso significa que eu posso manipular | |
// o retorno como um objeto. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment