Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to convert for of loop into a for loop to solve the ESLint error

How do I convert a for of loop into a for loop?
This is so that I can avoid/ solve the eslint error message.I have tried googling, but the solution I am getting is to disable/configure eslint. Help me understand what I’m missing.
here is the error message.
" error iterators/generators require regenerator-runtime, which is too heavyweight for this guide to allow them. Separately, loops should be avoided in favor of array iterations "

here is my working code using the for of.

let str = '';
const arr = [];
for (const person of featuredObject) {
  str = `        
    <div class="portfolio"><img src=${person.image} alt="#"></div>
    <div class="card-one">
        <h3 class="name">${person.Name}</h3>
        <p class="myTitle">${person.title}</p>
        <p class="myDescription">${person.description}</p>
    </div>
</div>`;
  arr.push(str);
}

here is my failed implementation of the for loop.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

let str = '';
const person = '';
const arr = [];
for (let i = 0; i < featuredObject.length; i += 1) {
  str= `
    <div class="portfolio"><img src=${person.image} alt="#"></div>
    <div class="card-one">
        <h3 class="name">${person.Name}</h3>
        <p class="myTitle">${person.title}</p>
        <p class="myDescription">${person.description}</p>
    </div>
</div>`;
  arr.push(str);
}

>Solution :

let str = "";
const arr = [];
for (let i = 0; i < featuredObject.length; i += 1) {
  const person = featuredObject[i];
  str = `
    <div class="portfolio"><img src=${person.image} alt="#"></div>
    <div class="card-one">
        <h3 class="name">${person.Name}</h3>
        <p class="myTitle">${person.title}</p>
        <p class="myDescription">${person.description}</p>
    </div>
  </div>`;
  arr.push(str);
}

Better way

const arr = featuredObject.map((person) => {
  const str = `
    <div class="portfolio"><img src=${person.image} alt="#"></div>
    <div class="card-one">
        <h3 class="name">${person.Name}</h3>
        <p class="myTitle">${person.title}</p>
        <p class="myDescription">${person.description}</p>
    </div>
  </div>`;
  return str;
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading