• for 循环
const obj = {
    id:1,
    name:'zhangsan',
    age:18
}

for(let key  in obj){
    console.log(key + '---' + obj[key])
}

 

输出结果:

id --- 1
name --- zhangsan
age --- 18

 

  • Object.keys(obj)
  • Object.values(obj)
const obj = {
    id:1,
    name:'zhangsan',
    age:18
}
console.log(Object.keys(obj))
console.log(Object.values(obj))

 

输出结果: obj对象的key组成的数组

['id','name','age']

输出结果:obj对象的value组成的数组

['1','zhangsan','18']

 

  • Object.getOwnPropertyNames(obj)
const obj = {
    id:1,
    name:'zhangsan',
    age:18
}
Object.getOwnPropertyNames(obj).forEach(function(key){
    console.log(key+ '---'+obj[key])
})

 

输出结果:

id --- 1
name --- zhangsan
age --- 18

posted on 2020-12-18 13:58  FuYingju  阅读(37)  评论(0编辑  收藏  举报