Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

80
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda