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
This method was edited on February 7, 2021. The v...
Click here to return to the 123WORDPRESS.COM HTML ...
Here are some examples of how I use this property ...
Table of contents introduce Usage scenarios Sourc...
In the previous article, I introduced the detaile...
Nginx's configuration syntax is flexible and ...
1. Introduction As we all know, in the applicatio...
Note 1: The entire background in the above pictur...
background Getting the slow query log from mysql....
Preface In a common business scenario, we need to...
Apple Mug Icons and Extras HD StorageBox – add on...
As the domestic network environment continues to ...
Use the find command to find files larger than a ...
Linux task management - background running and te...
I would like to ask a question. In Dreamweaver, I...