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

163
Vistas
replace /g not working when used with map method

I have a set of arrays and I'm trying to change the dash on the dates to a slashes but the replace /g is not working when I use it in a map method.

let set1 = [['Jones', 'Ann', 'F', '6-3-1975', 'Red'],
['Perez', 'Maria', 'F', '4-2-1979', 'Green'],
['Samuels', 'Rika', 'M', '12-2-1973', 'Black']]

set1.map(arr => arr[3].replace(/-/g, "/"))
console.log(set1)

The result should be as shown bellow but the dashes are not changing

['Jones', 'Ann', 'F', '6/3/1975', 'Red'],
['Perez', 'Maria', 'F', '4/2/1979', 'Green'],
['Samuels', 'Rika', 'M', '12/2/1973', 'Black']
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Array.map does not mutate the original array. You have to assign the result after mapping back to set1.

You'll also have to assign the result after replacing back to the item at index 3 and return arr in the map function.

let set1 = [['Jones', 'Ann', 'F', '6-3-1975', 'Red'],
['Perez', 'Maria', 'F', '4-2-1979', 'Green'],
['Samuels', 'Rika', 'M', '12-2-1973', 'Black']]

set1 = set1.map(arr => (arr[3] = arr[3].replace(/-/g, "/"), arr))
console.log(set1)

If you don't want to create a new one, use Array.forEach:

let set1 = [['Jones', 'Ann', 'F', '6-3-1975', 'Red'],
['Perez', 'Maria', 'F', '4-2-1979', 'Green'],
['Samuels', 'Rika', 'M', '12-2-1973', 'Black']]

set1.forEach(arr => arr[3] = arr[3].replace(/-/g, "/"))
console.log(set1)

about 4 years ago · Juan Pablo Isaza Denunciar

0

Another answer, but using forEach instead. This may use less memory compared to the map approach.

let set1 = [['Jones', 'Ann', 'F', '6-3-1975', 'Red'],
['Perez', 'Maria', 'F', '4-2-1979', 'Green'],
['Samuels', 'Rika', 'M', '12-2-1973', 'Black']]

set1.forEach(arr => arr[3] = arr[3].replace(/-/g, "/"))

console.log(set1)

about 4 years ago · Juan Pablo Isaza Denunciar

0

The .map() function expects you to return the entire element you want to be in the resulting array. So, you need to return the entire arr value, not just the date string.

let set1 = [
  ['Jones', 'Ann', 'F', '6-3-1975', 'Red'],
  ['Perez', 'Maria', 'F', '4-2-1979', 'Green'],
  ['Samuels', 'Rika', 'M', '12-2-1973', 'Black']
];

set1 = set1.map(arr => {
  arr[3] = arr[3].replace(/-/g, "/");
  return arr;
});

console.log(set1);

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