Skip to content

Commit c32f6f7

Browse files
committed
fixes
1 parent 2b2940f commit c32f6f7

File tree

1 file changed

+84
-29
lines changed

1 file changed

+84
-29
lines changed

article.md

Lines changed: 84 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -92,35 +92,6 @@ map.set('1', 'str1')
9292
```
9393
````
9494
95-
## Map from Object
96-
97-
When a `Map` is created, we can pass an array (or another iterable) with key-value pairs for initialization, like this:
98-
99-
```js
100-
// array of [key, value] pairs
101-
let map = new Map([
102-
['1', 'str1'],
103-
[1, 'num1'],
104-
[true, 'bool1']
105-
]);
106-
```
107-
108-
If we have a plain object, and we'd like to create a `Map` from it, then we can use built-in method [Object.entries(obj)](mdn:js/Object/entries) that returns an array of key/value pairs for an object exactly in that format.
109-
110-
So we can initialize a map from an object like this:
111-
112-
```js
113-
let obj = {
114-
name: "John",
115-
age: 30
116-
};
117-
118-
*!*
119-
let map = new Map(Object.entries(obj));
120-
*/!*
121-
```
122-
123-
Here, `Object.entries` returns the array of key/value pairs: `[ ["name","John"], ["age", 30] ]`. That's what `Map` needs.
12495
12596
## Iteration over Map
12697
@@ -168,6 +139,90 @@ recipeMap.forEach( (value, key, map) => {
168139
});
169140
```
170141
142+
## Object.entries: Map from Object
143+
144+
When a `Map` is created, we can pass an array (or another iterable) with key/value pairs for initialization, like this:
145+
146+
```js run
147+
// array of [key, value] pairs
148+
let map = new Map([
149+
['1', 'str1'],
150+
[1, 'num1'],
151+
[true, 'bool1']
152+
]);
153+
154+
alert( map.get('1') ); // str1
155+
```
156+
157+
If we have a plain object, and we'd like to create a `Map` from it, then we can use built-in method [Object.entries(obj)](mdn:js/Object/entries) that returns an array of key/value pairs for an object exactly in that format.
158+
159+
So we can create a map from an object like this:
160+
161+
```js run
162+
let obj = {
163+
name: "John",
164+
age: 30
165+
};
166+
167+
*!*
168+
let map = new Map(Object.entries(obj));
169+
*/!*
170+
171+
alert( map.get('name') ); // John
172+
```
173+
174+
Here, `Object.entries` returns the array of key/value pairs: `[ ["name","John"], ["age", 30] ]`. That's what `Map` needs.
175+
176+
177+
## Object.fromEntries: Object from Map
178+
179+
We've just seen how to create `Map` from a plain object with `Object.entries(obj)`.
180+
181+
There's `Object.fromEntries` method that does the reverse: given an array of `[key, value]` pairs, it creates an object from them:
182+
183+
```js run
184+
let prices = Object.fromEntries([
185+
['banana', 1],
186+
['orange', 2],
187+
['meat', 4]
188+
]);
189+
190+
// now prices = { banana: 1, orange: 2, meat: 4 }
191+
192+
alert(prices.orange); // 2
193+
```
194+
195+
We can use `Object.fromEntries` to get an plain object from `Map`.
196+
197+
E.g. we store the data in a `Map`, but we need to pass it to a 3rd-party code that expects a plain object.
198+
199+
Here we go:
200+
201+
```js run
202+
let map = new Map();
203+
map.set('banana', 1);
204+
map.set('orange', 2);
205+
map.set('meat', 4);
206+
207+
*!*
208+
let obj = Object.fromEntries(map.entries()); // make a plain object (*)
209+
*/!*
210+
211+
// done!
212+
// obj = { banana: 1, orange: 2, meat: 4 }
213+
214+
alert(obj.orange); // 2
215+
```
216+
217+
A call to `map.entries()` returns an array of key/value pairs, exactly in the right format for `Object.fromEntries`.
218+
219+
We could also make line `(*)` shorter:
220+
```js
221+
let obj = Object.fromEntries(map); // omit .entries()
222+
```
223+
224+
That's the same, because `Object.fromEntries` expects an iterable object as the argument. Not necessarily an array. And the standard iteration for `map` returns same key/value pairs as `map.entries()`. So we get a plain object with same key/values as the `map`.
225+
171226
## Set
172227
173228
A `Set` is a special type collection - "set of values" (without keys), where each value may occur only once.

0 commit comments

Comments
 (0)