Iterables are iterable objects (like Arrays).
Iterables can be accessed with simple and efficient code.
Iterables can be iterated over with for..of
loops
The JavaScript for..of
statement loops through the elements of an iterable object.
for (variable of iterable) {
//
code block to be executed
}
Iterating is easy to understand.
It simply means looping over a sequence of elements.
Here are some easy examples:
You can use a for..of
loop to iterate over the elements of a string:
const name = "91xjr";
for (const x of name) {
//
code block to be executed
}
Try it Yourself »
You can use a for..of
loop to iterate over the elements of an Array:
const letters = ["a","b","c"];
for (const x of letters) {
//
code block to be executed
}
Try it Yourself »
You can learn more details about Iterables in the chapter JS Object Iterables.
You can use a for..of
loop to iterate over the elements of a Set:
const letters = new Set(["a","b","c"]);
for (const x of letters) {
//
code block to be executed
}
Try it Yourself »
Sets and Maps are covered in the next chapters.
You can use a for..of
loop to iterate over the elements of a Map:
const fruits = new Map([
["apples", 500],
["bananas", 300],
["oranges", 200]
]);
for (const x of fruits) {
//
code block to be executed
}
Try it Yourself »
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!