have. Traversal method: 1. "for...in" statement, traverses the object's own and inherited enumerable properties; 2. Object.keys(), traverses the property names; 3. Object.getOwnPropertyNames(), traverses the properties Name to traverse; 4. Object.getOwnPropertySymbols(), traverse all Symbol properties; 5. Reflect.ownKeys(), traverse all properties.
The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.
(1) for...in
for. ..in
Loop through the object's own and inherited enumerable properties (excluding Symbol properties).
const obj = { id:1, name:‘zhangsan’, age:18 } for(let key in obj){ console.log(key + ‘—’ + obj[key]) }
(2)Object.keys(obj)
Object.keys
Return An array containing the keys of all enumerable properties (excluding Symbol properties) of the object itself (excluding inherited ones).
(3) Object.getOwnPropertyNames(obj)
Object.getOwnPropertyNames
Returns an array containing The key names of all properties of the object itself (excluding Symbol properties, but including non-enumerable properties).
(4) Object.getOwnPropertySymbols(obj)
Object.getOwnPropertySymbols
Returns an array containing Key names for all Symbol properties of the object itself.
(5) Reflect.ownKeys(obj)
Reflect.ownKeys
Returns an array containing All key names of the object itself (excluding inherited ones), regardless of whether the key name is a Symbol or a string, and whether it is enumerable or not.
The above 5 methods traverse the key names of the object and all follow the same order rules of attribute traversal.
Finally traverse all Symbol keys and sort them in ascending order according to their joining time.
(5) Reflect.enumerate(obj)
Reflect.enumerate(obj), returns an Iterator object, traverses the sum of the object itself All inherited enumerable properties (excluding Symbol properties) are the same as the for...in loop.
【Related recommendations: javascript video tutorial, programming video】
The above is the detailed content of Is there a way to traverse objects in es6?. For more information, please follow other related articles on the PHP Chinese website!