-
字符串遍历
for…of --ES6语法,可以遍历Array、Set、Map、String、TypedArray、arguments等可迭代对象,可以使用break、continue
for(let char of string){ console.log("字符:"+char); }
-
数组遍历
-
for
和for/in
,可以访问数组的下标,而不是实际的数组元素值。 -
使用
for/of
,则可以直接访问数组的元素值。使用
for/of
获取数组索引,可以这样写:for (const [i, v] of arr.entries()) { console.log(i, v); }
-
使用
forEach()
,则可以同时访问数组的下标与元素值尽量不要在
forEach
中使用 aysnc/await 以及 generators。arr.forEach((v, i) => console.log(v));
-
-
for/in
与forEach
会跳过空元素,数组中的空元素被称为“holes”。
js遍历方式!!!!
最新推荐文章于 2024-04-24 00:45:00 发布