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

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

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 Denunciar

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 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