Home >Web Front-end >Front-end Q&A >What is the method of traversing objects in es6
Method: 1. Use "Object.keys(obj)" to traverse the object and return a key array including all enumerable properties of the object itself; 2. Use "Reflect.ownKeys(obj)" to traverse the object , returns an array containing all key names of the object itself; 3. Use "for in" to loop through the object, etc.
The operating environment of this tutorial: Windows 10 system, ECMAScript version 6.0, Dell G3 computer.
1. for…in
for…in loop traverses the object’s own and inherited enumerables Properties (excluding Symbol properties).
2. Object.keys(obj)
Object.keys returns an array, including all enumerable properties of the object itself (excluding inherited ones) (not Key name containing Symbol attribute).
3. Object.getOwnPropertyNames(obj)
Object.getOwnPropertyNames returns an array containing all properties of the object itself (excluding Symbol properties, but including non-enumerable attribute) key name.
4. Object.getOwnPropertySymbols(obj)
Object.getOwnPropertySymbols returns an array containing the key names of all Symbol properties of the object itself.
5. Reflect.ownKeys(obj)
Reflect.ownKeys returns an array containing all the key names of the object itself, regardless of whether the key name is a Symbol or a string. It doesn't matter whether it is enumerable or not.
The above 5 methods to traverse the key names of objects all follow the same order rules of attribute traversal.
First traverse all numeric keys and sort them in ascending order of value.
Secondly, traverse all string keys and sort them in ascending order by joining time.
Finally traverse all Symbol keys and arrange them in ascending order according to the joining time.
Reflect.ownKeys({ [Symbol()]:0, b:0, 10:0, 2:0, a:0 }) // [‘2′, ’10’, ‘b’, ‘a’, Symbol()]
[Related recommendations: javascript video tutorial, web front-end】
The above is the detailed content of What is the method of traversing objects in es6. For more information, please follow other related articles on the PHP Chinese website!