Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

164
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda