1. Self-enumerable properties This is reasonable, because most of the time you only need to care about the properties of the object itself. Let's look at an example where an object has both own and inherited properties, let simpleColors = { colorA: 'white', colorB: 'black' }; let natureColors = { colorC: 'green', colorD: 'yellow' }; Object.setPrototypeOf(natureColors, simpleColors); Object.keys(natureColors); // => ['colorC', 'colorD'] natureColors['colorA']; // => 'white' natureColors['colorB']; // => 'black'
// ... Object.values(natureColors); // => ['green', 'yellow'] Object.entries(natureColors); // => [ ['colorC', 'green'], ['colorD', 'yellow'] ] Now note the difference with the // ... let enumerableKeys = []; for (let key in natureColors) { enumerableKeys.push(key); } enumerableKeys; // => ['colorC', 'colorD', 'colorA', 'colorB'] The In addition, for..in also traverses the properties inherited from 2. Object.values() returns property values For example, use Object.keys() to collect keys, and then use the key to get the corresponding value from the object: let meals = { mealA: 'Breakfast', mealB: 'Lunch', mealC: 'Dinner' }; for (let key of Object.keys(meals)) { let mealName = meals[key]; // ... do something with mealName console.log(mealName); } // 'Breakfast' 'Lunch' 'Dinner' meal is a plain object. Use The code looks simple, but let meals = { mealA: 'Breakfast', mealB: 'Lunch', mealC: 'Dinner' }; for (let mealName of Object.values(meals)) { console.log(mealName); } // 'Breakfast' 'Lunch' 'Dinner' Because 3. Object.entries() It may not be very convenient to use these key-value pairs directly, but it becomes very easy to access keys and values through array destructuring assignment, as shown below: let meals = { mealA: 'Breakfast', mealB: 'Lunch', mealC: 'Dinner' }; for (let [key, value] of Object.entries(meals)) { console.log(key + ':' + value); } // 'mealA:Breakfast' 'mealB:Lunch' 'mealC:Dinner' As shown above, A two-dimensional array of key-value pairs can be converted into a Map object using the regular Map constructor. Here is an example to slow down your pace: let greetings = { morning: 'Good morning', midday: 'Good day', evening: 'Good evening' }; let greetingsMap = new Map(Object.entries(greetings)); greetingsMap.get('morning'); // => 'Good morning' greetingsMap.get('midday'); // => 'Good day' greetingsMap.get('evening'); // => 'Good evening' Map objects store key-value pairs. Any value (object or primitive) can be used as a key or a value. Interestingly, Map provides methods that are equivalent to
Let's look at the methods that return a map of .values() and .entries(): // ... [...greetingsMap.values()]; // => ['Good morning', 'Good day', 'Good evening'] [...greetingsMap.entries()]; // => [ ['morning', 'Good morning'], ['midday', 'Good day'], // ['evening', 'Good evening'] ] Note: 4. Order of object propertiesJS objects are simple key-value mappings, therefore, the order of properties in an object is insignificant and should not be relied upon in most cases. In ES5 and earlier standards, the order of properties was not specified at all. However, starting with ES6, the order of properties is based on a specific set of rules, unless time is specifically specified. We will use two new methods
If you need an ordered collection, it is recommended to store the data in an array or Set. Summarize: Object.entries() is best used for array destructuring in a way that keys and values can be easily assigned to different variables. This function also makes it easy to map plain JS object properties into a Map object. , Note: The order in which There is no way to know in real time This concludes this article about several ways to easily traverse object properties in JS. For more information about several ways to easily traverse object properties in JS, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Implementation of docker-compose deployment of zk+kafka+storm cluster
>>: MySql sets the specified user database view query permissions
Scenario: When starting tomcat in docker (version...
Copy code The code is as follows: <!DOCTYPE ht...
Table of contents Preface analyze Initial Renderi...
In MySQL, the LOAD_FILE() function reads a file a...
1. I purchased a VPS and CentOS system, and found...
Three MySQL instance processes are started on one...
When developing a backend management project, it ...
Table of contents 1. Use SVG 2. Use fontAwesome 3...
The specific code of the sliding button made with...
After I analyzed the Taobao details page last time...
Table of contents Preface The principle of browse...
Front-end test page code: <template> <di...
Introduction to Nginx dynamic and static separati...
This article mainly introduces the principle and ...
Use native JS to write a nine-square grid to achi...