I have an array:
let people = ['lucy', 'jerry', 'ricky', 'jessy', 'jerry', 'phil']
And I want to capitalize every word except jerry. How would I go about this?
I know how to map through the array:
let output = people.map(person => {
return person.toUpperCase()
}
console.log(output)
Obviously, that returns the entire array with every word capitalized. But again, what if I wanted to capitalize all of the words in the array except jerry, which is written twice?
Here is a quick and scalable method. Set the exclusions in their own array and just check for them in the map loop.
let people = ['lucy', 'jerry', 'ricky', 'jessy', 'jerry', 'phil']
let exclude=['jerry'];
let output = people.map(person => exclude.includes(person) ? person : person.toUpperCase())
console.log(output)
let output2 = people.map(person => exclude.includes(person) ? person : person[0].toUpperCase() + person.slice(1))
console.log(output2)