我们可以使用for-of循环访问数组元素:
for (const j of [1, 2, 3, 4, 5]) {
console.log(j);
}
Run Code Online (Sandbox Code Playgroud)
如何修改此代码以访问当前索引?我想使用for-of语法实现这一点,既不是forEach也不是for-in.
When using a for of loop, both of these are allowed and work:
const numbers = [1,2,3];
// works
for(let number of numbers) {
console.log(number);
}
// also works
for(const number of numbers) {
console.log(number);
}
Run Code Online (Sandbox Code Playgroud)
I always use const since I annot fanthom changing the number variable in any context, but when I see a for...of loop in other people's code, it often uses let. Maybe there's a drawback to const that I didn't see? Browser bugs?
为什么在循环中使用 …
我使用ESLint作为我的ES6程序,使用AirBNB规则集.由于充分和充分的原因,我for...of在我的代码中使用构造,但ESLint对它反对,发出no-restricted-syntax错误.
http://eslint.org/docs/rules/no-restricted-syntax上的文档解释了我如何在我的.eslint文件中指定它所反对的语法树节点集:例如,如果我不喜欢的是with语句, 我可以用:
"无限制语法":["警告","WithStatement"]
但是我不想指定一整套未经批准的结构,我只想说我认为这样的结构可以.在概念上类似的东西
ESlint.rules [ '没有限制的语法'] removeEntry( 'ForOfStatement');
有没有办法在ESLint文件中执行此操作?或者,如果失败了,至少有一种方法可以让它告诉我它当前的no-restricted-syntax配置是什么,所以我可以从中手动删除ForOfStatement吗?
我想设置options[Symbol.iterator]属性,以便使用for...of语句迭代我创建的简单对象:
options = {
male: 'John',
female: 'Gina',
rel: 'Love'
};
for(let p of options){
console.log(`Property ${p}`);
};
Run Code Online (Sandbox Code Playgroud)
但是这段代码给了我以下错误:
array.html:72 Uncaught TypeError: options[Symbol.iterator] is not a function
Run Code Online (Sandbox Code Playgroud)
如何在上面的简单对象上设置正确的迭代器函数?
解决了
// define the Iterator for the options object
options[Symbol.iterator] = function(){
// get the properties of the object
let properties = Object.keys(this);
let count = 0;
// set to true when the loop is done
isDone = false;
// define the next method, need for iterator
let …Run Code Online (Sandbox Code Playgroud) 我无意中看到这会导致 V8(Chrome、Node.js 等)中的错误:
for (let val of Symbol()) { /*...*/ }
Run Code Online (Sandbox Code Playgroud)
类型错误:符号不是函数或其返回值不可迭代
似乎任何其他不可迭代的值(包括函数)会导致另一个错误:
for (let val of function () { throw 'never called' }) { /*...*/ }
Run Code Online (Sandbox Code Playgroud)
类型错误:(中间值)不可迭代
正如参考资料所述,该错误特定于 Chrome:
类型错误:“x”不是函数或其返回值不可迭代(Chrome)
...
作为 for...of 的右侧或作为 Promise.all 或 TypedArray.from 等函数的参数给出的值不是可迭代对象。可迭代对象可以是内置的可迭代类型,例如 Array、String 或 Map、生成器结果或实现可迭代协议的对象。
似乎没有列出的东西会接受一个函数而不是 iterable 作为参数,所以不清楚为什么错误强调函数类型。
这个错误有什么意义吗?在什么情况下,is not a function评论在其上下文中有意义?
我在研究 For/in 和 For/of 循环时遇到术语 Iterable 和 Enumerable。对象应该是可枚举的,我们必须使用 For/in 循环来循环对象的属性,并使用 For/of 来循环数组和字符串。我无法理解这两个术语。这两者有什么区别?
我可以在循环的and类型中使用breakandcontinue语句吗?或者它们只能在常规循环内访问。for...infor...offor
例子:
myObject = {
propA: 'foo',
propB: 'bar'
};
for (let propName in myObject) {
if (propName !== 'propA') {
continue;
}
else if (propName === 'propA') {
break;
}
}
Run Code Online (Sandbox Code Playgroud) 这两者是相同的还是可以互换的?
在哪些用例中,人们会选择其中一种而不是另一种?
for(let i of array){some code}
Run Code Online (Sandbox Code Playgroud)
for(let i = 0; i < array.length; i++){some code}
Run Code Online (Sandbox Code Playgroud)
例子
完成该解决方案,以便该函数将使用单词之间的空格来分解驼峰式大小写。
solution("camelCasing") == "camel Casing"
如果我们使用这个例子,除了性能优势(如果有的话)之外,为什么有人会使用一个循环而不是另一个循环?
如果有任何其他示例/实例,那就太好了。
I'm trying to edit an array and remove elements that do not meet a certain condition. If I use a reverse for loop combined with .splice(index,n), the code works just fine. I'm stuck at implementing the same using the ES6 for...of loop
let array=[1,2,3];
//reverse for loop
for(var i=array.length - 1; i>=0; i--) {
if(array[i]>=2) {
/*Collect element before deleting it.*/
array.splice(i,1);
}
}
console.log(array);
// returns [1]
Run Code Online (Sandbox Code Playgroud)
Using for..of
let array=[1,2,3];
for(let entry of array) {
if(entry>=2) { …Run Code Online (Sandbox Code Playgroud) for...of当我中断循环时,是否可以循环迭代器的一部分而不关闭迭代器?
例子:
function* numbers(i=0){
while(true) yield i++;
}
let nums=numbers();
// this loop prints numbers from 0 to 3
for(const n of nums){
if(n>3) break;
console.log(n);
}
// this loop doesn't print anything because `nums` has already been closed
for(const n of nums){
if(n>10) break;
console.log(n);
}Run Code Online (Sandbox Code Playgroud)
我知道我可以通过自己的调用来遍历迭代器iterator.next()。但我想知道是否可以用for...of语法来做到这一点。
for-of-loop ×10
javascript ×9
ecmascript-6 ×5
for-in-loop ×2
iterator ×2
arrays ×1
debugging ×1
enumerable ×1
eslint ×1
for-loop ×1
iterable ×1
loops ×1
node.js ×1
object ×1
string ×1
v8 ×1