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

212
Vistas
How to replace certain strings in a single go from a string?

I have this function here that returns a function. The job it does is that it receives two or more arguments and it saves them in an object. If the number of arguments is equal to one then we find all the words in the string that match the stored keys in that object and replace them with their values. I have managed to do it but I want to achieve this in a single go. At this moment the keys are being replaced with values one by one because I am using a for loop to iterate. Is there a better way to do it so that we can replace all keys in one go?

function censor() {
  let newObj = {};
  return function(...args) {
    if (args.length > 1) {
      newObj[args[0]] = args[1];
      return newObj;
    } else {
      for (let key in newObj) {
        console.log(args[0] = args[0].replace(args[0].match(key), newObj[key]));
      };
    }
  }
}

const changeScene = censor();
console.log(changeScene('dogs', 'cats'));
console.log(changeScene('quick', 'slow'));
changeScene('The quick, brown fox jumps over the lazy dogs.');

about 4 years ago · Juan Pablo Isaza
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