Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

93
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!