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

92
Vistas
Iterate through an array and excecute functions in order javascript

I'm struggling trying to iterate through an array while updating some content(string). Currently, I have this code:

const replaceUrls = async (content: string) => {
  let allImgsUpdated = 0;
  let newContent = content;
  const oldImgs = content.match(/https?:\/\/.*\.(?:png|jpg|jpeg|svg|gif)/gi);
  const newImages = ["first", "second"];

  if (oldImgs) {
    oldImgs.forEach((old, idx) => {
      try {
        newContent.replace(old, newImages[idx]);
      } finally {
        allImgsUpdated++;
      }
    });
  }

  if (oldImgs && oldImgs?.length >= allImgsUpdated) return newContent;
};

My objective is to update the newContent two oldImages urls (this is a test, later I will update the images to be dynamic to how much 'oldImages' I have) to the new ones. I did the allImgsUpdated workaround so I can return the newContent when the forEach already finished

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I solved it with this workaround:

const replaceUrls = async (content: string) => {
  let allImgsUpdated = 0;
  let newContent = content;
  const oldImgs = content.match(/https?:\/\/.*\.(?:png|jpg|jpeg|svg|gif)/gi);
  const newImages = ["primera", "segunda"];

  if (oldImgs) {
    oldImgs.forEach((old, idx) => {
      try {
        const updatedContent = newContent.replace(old, newImages[idx]);
        return (newContent = updatedContent);
      } finally {
        allImgsUpdated++;
      }
    });
  }

  if (oldImgs && oldImgs?.length >= allImgsUpdated) return newContent;
};
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