1. for ... in 语句
for (let variable in object) { ... }
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/for...in
以任意次序迭代一个对象的可枚举属性。
[html] view plain copy
- var obj = {a: 1, b: 2};
- for (let i in obj) {
- console("key:" + i + ", value:" + obj[i]);
- }
数组Array的遍历,i则为数字
2. 迭代器遍历
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Iterator
如Array
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
[html] view plain copy
- arr.forEach((value, index) => {
- console.log(value);
- });
3. for ... of 语句
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/for...of
for...of语句在可迭代对象(包括 Array, Map, Set, String, TypedArray,arguments 对象等等)上创建一个迭代循环,对每个不同属性的属性值,调用一个自定义的有执行语句的迭代挂钩.
但对于普通对象是不适应的。原因是没有实现参考下文。
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Guide/iterable
原文地址:https://www.cnblogs.com/ziyoublog/p/9130751.html
时间: 2024-10-09 11:22:49