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

223
Visualizações
Replace words with array map

I have this array

(2) ['beginning=beginner', 'leaves=leave']

and this string

its beginner in the sounds leave

which i have converted to an array

var words = text.split(' ');

i want to replace beginner with beginning and leave with leaves it can be any dynamic words but for now it has only two elements i can replace it within for loop. Is it possible with map method.

this.words.map((words, i) => console.log(words));

Note: Only first instance should get replaced.

Any Solution Thanks

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

does this correct with your question ?

const arrString = ["beginning=beginner", "leaves=leave", "sound=sounds"];

let string = "its beginner in the sounds leave";

arrString.forEach((mapString) => {
  const stringArr = mapString.split("=");
  string = string.replace(stringArr[1], stringArr[0]);
});

console.log("string", string);
// "its beginning in the sound leaves"
about 4 years ago · Juan Pablo Isaza Relatório

0

You can do it in without a nested loop too if you compromise with space.

Here is a solution which creates a mapping object for replacement values and uses array methods like map(),forEach(),join() and string method like split()

const arrString = ["beginning=beginner", "leaves=leave", "sound=sounds"];

let string1 = "its beginner in the sounds leave beginner";
const replaceObj = {};
const arrBreaks = arrString.forEach(x => {
let els = x.split("=");
replaceObj[els[1]] = els[0]; });

const ans = string1.split(' ').map((x) => {
  if(x in replaceObj) { let val = replaceObj[x]; delete val; return val; }
  return x;
}).join(' ');

console.log(ans);

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