Skip to content

Instantly share code, notes, and snippets.

@brenofreire
Created February 25, 2020 16:15
Show Gist options
  • Save brenofreire/5e73000606af227801b87a036b669886 to your computer and use it in GitHub Desktop.
Save brenofreire/5e73000606af227801b87a036b669886 to your computer and use it in GitHub Desktop.
Entendendo melhor o "reduce" de array do JavaScript
// 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