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

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

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