js中 for in 与 for of 区别
2020-03-26 本文已影响0人
AI时代岁月笔记
for in是ES5标准,遍历key. (遍历对象返回key, 遍历数组项返回数组的index)
for of是ES6标准,遍历value.
例:
let myobj={
arr:[
{id:1, name:"张三", remark:"100,30"},
{id:2, name:"李四", remark:"100,40"},
{id:3, name:"赵四", remark:"200,130"},
{id:4, name:"钱三", remark:"300,30"},
{id:5, name:"孟敏", remark:"400,50"},
]
}
for(let i in myobj){
console.log(i); //返回:arr
}
for(let i in myobj.arr){
console.log(i); //返回:0,1,2,3,4
}
for(let i of myobj.arr){
console.log(i); //返回如下结果列表
}
{ id: 1, name: '张三', remark: '100,30' }
{ id: 2, name: '李四', remark: '100,40' }
{ id: 3, name: '赵四', remark: '200,130' }
{ id: 4, name: '钱三', remark: '300,30' }
{ id: 5, name: '孟敏', remark: '400,50' }