Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

70
Views
Capitalise names - Mutate original array

I have the following array:

const people = [
    "JoHn", "ChrISTiana", "anThoNY", "MARia", "jaMeS", "MIChaEl", "jeNNIFeR"
];

I want to capitalise the first letter of each word and lowercase the rest.

I have used the following function to generate a new array:

let capitaliseNames = (arr) => {
  return arr.map((item) => item[0].toUpperCase() + item.slice(1).toLowerCase());
};

But how do I mutate the original array? I have tried the same approach but using forEach and it doesn't seem to work.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

This will do the work

let capitaliseNames = (arr) => { arr.forEach((elem, i) => { arr[i] = elem[0].toUpperCase() + elem.slice(1).toLowerCase() }); };
about 4 years ago · Juan Pablo Isaza Report

0

First: toLowerCase() then you can use a custom function for the mutation the first letter.

UPDATE Use a loop where you can manipulate the original array. For example a for loop.

const people = ["JoHn", "ChrISTiana", "anThoNY", "MARia", "jaMeS", "MIChaEl", "jeNNIFeR"];

for(let i = 0; i < people.length; i++) {  
   people[i] = capitalizeFirstLetter( people[i].toLowerCase() ) ;
}

function capitalizeFirstLetter(str) {
  return str[0].toUpperCase() + str.slice(1);
}

console.log(people)

about 4 years ago · Juan Pablo Isaza Report

0

Use forEach() instead of map() and reassign the mutated value back to the original array:

const people = ["JoHn", "ChrISTiana", "anThoNY", "MARia",
                "jaMeS", "MIChaEl", "jeNNIFeR"];

const capitaliseNames = (arr) => arr.forEach(
  (v, i, a) => a[i] = v[0].toUpperCase() + v.slice(1).toLowerCase()
);

capitaliseNames(people);

console.log(people);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!