In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
In this article Xiaobian for you to introduce in detail the "es6 traversal of object properties of what are the methods", detailed content, clear steps, details handled properly, I hope that this "es6 traversal of object properties of what methods" article can help you solve doubts, the following follow the editor's ideas slowly in-depth, together to learn new knowledge.
There are six ways to traverse the object: 1, the "for...in" statement that iterates through the object's own and inherited enumerable properties; 2, keys () traverses the attribute name, values () traverses the attribute value; 3, getOwnPropertyNames (), returns an array containing all attributes, and so on.
The operating environment of this tutorial: windows7 system, ECMAScript version 6, Dell G3 computer.
Six methods of traversing object attributes by ES6
1 、 for...in
Loop through the object's own and inherited enumerable properties (excluding the symbol property)
For (let k in obj) {}
Loop through the object's own and inherited enumerable properties (loop through the object's own and inherited enumerable properties (excluding the Symbol property)
Let obj = {console.log (k+':'+obj [k])} / / 0:a//1:b//2:c
2. Object.keys (obj) | | Object.values (obj)
Returns an array of all enumerable properties of the object itself (excluding inheritance) (excluding the Symbol property)
Keys () is the traversal of the attribute name, and values () is the traversal of the attribute value
Let obj = [0 "," 1 "," 2 "] console.log (Object.values (obj)) / / [" a "," b "," c "]
3. Object.getOwnPropertyNames (obj)
Returns an array containing all the properties of the object itself (excluding attributes, but including non-enumerable properties)
Let obj = {'0mm console.log (key) {console.log (key,obj [key])); / / 0a / 1b / 2c
4. Object.getOwnPropertySymbols (obj)
Returns an array containing all the Symbol properties of the object itself
Example: get the symbol value attribute of the object
Var obj = {}; var a = Symbol ("a"); var b = Symbol.for ("b"); obj [a] = "localSymbol"; obj [b] = "globalSymbol"; var objectSymbols = Object.getOwnPropertySymbols (obj); console.log (objectSymbols.length); / / 2console.log (objectSymbols) / / [Symbol (a), Symbol (b)] console.log (objectSymbols [0]) / / Symbol (a)
5. Reflect.ownKeys (obj)
Returns an array containing all the properties of the object itself, whether the property name is Symbol or a string, or enumerable.
Const object1 = {property1: 42, property2: 13}; const array1 = []; console.log (Reflect.ownKeys (object1)); / / expected output: Array ["property1", "property2"] console.log (Reflect.ownKeys (array1)); / / expected output: Array ["length"]
6. Reflect.enumerate (obj)
Returns an Iterator object, traversing all enumerable properties of the object itself and inheritance (excluding the Symbol property), and for. The in loop is the same.
Var obj = {x: 1, y: 2}; for (var name of Reflect.enumerate (obj)) {console.log (name) } / / logs "x" and "y" here, this article "what are the methods of es6 traversing object properties" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it before you can understand it. If you want to know more about related articles, welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.