Skip to content

Instantly share code, notes, and snippets.

@deadkff01
Last active June 4, 2018 18:06
Show Gist options
  • Save deadkff01/0dfd9723ed79e804a900c6c3bf3bf148 to your computer and use it in GitHub Desktop.
Save deadkff01/0dfd9723ed79e804a900c6c3bf3bf148 to your computer and use it in GitHub Desktop.
JavaScript - Map function with reduce
const names = ['pikachu', 'squirtle', 'charmander']
const capitalize = function (name) {
const capitalizedFirst = name[0].toUpperCase()
const rest = name.slice(1)
return [capitalizedFirst].concat(rest).join('')
}
// map function with reduce
const map = (list, mapFunction) => {
return list.reduce((total, next) => {
total.push(mapFunction(next))
return total
}, [])
}
console.log(map(names, capitalize)) // ["Pikachu", "Squirtle", "Charmander"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment