for in,for of遍历


const obj = {
    name: 'Jack',
    age: '18',
    gender: '男'
}
for (key in obj) {
    console.log(key)//name age gender
    console.log(typeof key)//string string string
    console.log(obj[key])//Jack 18 男
    console.log(obj['key'])//undefined undefined undefined
}
 
const array=[2,4,6,8]
for(val in array){
    console.log(val)//0,1,2,3,(下标)
}
for(arr of array){
    console.log(arr)//2,4,6,8(遍历数组中的值)
}

 

posted @ 2021-07-12 19:53  苟苟苟  阅读(29)  评论(0编辑  收藏  举报