let obj = {
"id": 20201208,
"name": "sunshine",
"username": "sunshiny",
"hobby": ["music", "photography", "travel"],
"address": {
"street": "xx",
"city": {
"main": "KM",
"minor": "SZ"
}
}
}
for...in遍历
for (let item in obj) {
console.log(item); //item是对象属性名 id, name, username, hobby, address
}
Object.keys()
- 返回对象属性名组成的数组,然后可以使用数组的遍历方法进行遍历
Object.keys(obj) //["id", "name", "username", "hobby", "address"]
Object.getOwnPropertyNames()
Object.getOwnPropertyNames(obj) //["id", "name", "username", "hobby", "address"]
Reflect.ownKeys()
- 返回对象属性名组成的数组。Reflect是ES6为操作对象而提供的新API
Reflect.ownKeys(obj) //["id", "name", "username", "hobby", "address"]