[英]eslint use for loop or forEach
有 eslint 规则unicorn/no-array-for-each
( 链接)。
Use `for…of` instead of `Array#forEach(…)` unicorn/no-array-for-each
unicorn 规则的动机是它支持提前返回并声称更快。 对于像itemList = [{a:1},{a:2},{a:3}]
这样的数组。
for (const item of itemList) {
if (item.a > 1) break;
console.log(item.a)
}
但是,它会将 go 转换为 eslint 错误no-restricted-syntax
(链接)。
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 no-restricted-syntax
它说 for 循环是重量级的。 看起来这两个规则相互冲突,启用哪个更有意义?
正如@VLAX 所说,传统for
循环同时满足这两个规则。
for (let index = 0, l = itemList.length; index < l; index += 1) {
const item = itemList[index];
if (item.a > 1) break;
console.log(item.a)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.