JavaScript权威指南(JavaScript的子集和扩展)

1、for/each和for/in

区别:for/each并不是遍历对象的属性,而是遍历属性的值

//遍历对象
let  o = {one:1, two:2, three:3}
for(let p in o) console.log(p)  //'one' , 'two',  'three'
for each(let v in o) console.log(v)   //  1,2,3
//遍历数组
a =  ['one',  'two', 'three']
for(let p in a) console.log(p)  // 0 , 1 , 2
for each(let v  in a) console.log(v)  //'one', 'two', 'three'

猜你喜欢

转载自www.cnblogs.com/jun-web/p/simple-foreach.html