Home > Web Front-end > Front-end Q&A > Is there a way to traverse objects in es6?

Is there a way to traverse objects in es6?

青灯夜游
Release: 2022-10-25 19:09:19
Original
1592 people have browsed it

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.

Is there a way to traverse objects in es6?

The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.

es6 6 ways to traverse objects

(1) for...in

for. ..inLoop 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])
}
Copy after login

Is there a way to traverse objects in es6?

(2)Object.keys(obj)

Object.keysReturn An array containing the keys of all enumerable properties (excluding Symbol properties) of the object itself (excluding inherited ones).

Is there a way to traverse objects in es6?

(3) Object.getOwnPropertyNames(obj)

Object.getOwnPropertyNamesReturns an array containing The key names of all properties of the object itself (excluding Symbol properties, but including non-enumerable properties).

Is there a way to traverse objects in es6?

(4) Object.getOwnPropertySymbols(obj)

Object.getOwnPropertySymbolsReturns an array containing Key names for all Symbol properties of the object itself.

Is there a way to traverse objects in es6?

(5) Reflect.ownKeys(obj)

Reflect.ownKeysReturns 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.

Is there a way to traverse objects in es6?

The above 5 methods traverse the key names of the object and all follow the same order rules of attribute traversal.

  • First traverse all numeric keys and arrange 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 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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template