[英]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?如何将 for of 循环转换为 for 循环? 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.
这样我就可以避免/解决 eslint 错误消息。我尝试过谷歌搜索,但我得到的解决方案是禁用/配置 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 "
“错误迭代器/生成器需要 regenerator-runtime,这对于本指南来说太重了,无法允许它们。另外,应避免循环以支持数组迭代”
here is my working code using the for of.这是我使用 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.这是我失败的for循环实现。
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);
}
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;
});
In for of loop in person is a element of featuredObject, so it as all properties but in for loop person is string not an element featuredObject.在 for of 循环中人是特征对象的元素,因此它作为所有属性,但在 for 循环中人是字符串而不是元素特征对象。 I am using div element because if you append a element into another element it preserve all event listener but when you append something into innerHTML it removes all event listener.
我正在使用 div 元素,因为如果您将一个元素附加到另一个元素中,它会保留所有事件侦听器,但是当您将某些内容附加到 innerHTML 时,它会删除所有事件侦听器。
const arr = [];
for (let i = 0; i < featuredObject.length; i++) {
const person = featuredObject[i];
const div = document.createElement("div");
div.setAttribute("class", "portfolio");
div.innerHTML = `
<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>`;
arr.push(div);
}
Use map to generate the new array.使用map生成新数组。
const arr = featuredObject.map((person) => `
<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>`);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.