首页 > Web开发 > 详细

JS中的迭代 -- for of 与 for in

时间:2019-05-22 14:47:11      阅读:182      评论:0      收藏:0      [点我收藏+]

1 for...of:

迭代的是 可迭代的对象的value值。

1.1 array数组

let arrayList = [‘a‘, ‘b‘, ‘c‘];

for(let value of arrayList) {

// value =>  ‘a‘、 ‘b‘、 ‘c‘

}

1.2 string类型

let str = ‘abcde‘;

for(let value of str) {

value => a‘、 ‘b‘、 ‘c‘、‘d‘、‘e‘

}

 

1.3 可以迭代其他如: maps、 sets、 generators、 DOM节点集合、 arguments对象

 

2 for ... in

2.1 迭代对象的属性

let aObj = { aKey: ‘aValue‘, bKey: ‘bValue‘ };

for(let key in aObj) {

key => aKey  bKey

}

 

2.2 迭代获取数组或者string的下标

let str = ‘abcdefg‘;

for(let key in str) {

key => 0 1 2 3 4 5 6

}

 

JS中的迭代 -- for of 与 for in

原文:https://www.cnblogs.com/taishuhanmei/p/10905523.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!