- 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