Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 26c46a9

Browse files
committed
Keyed Collection - Map Complete
1 parent 3935a77 commit 26c46a9

File tree

1 file changed

+156
-0
lines changed

1 file changed

+156
-0
lines changed

‎05-Data-Structure.md

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,159 @@ fabThree.set(virat, "India").set(smith, "Australia").set(root, "England");
230230
```
231231

232232
The `set()` method maps each players with their specified country.
233+
234+
**Initialize a map with an iterable object**
235+
236+
We can pass an iterable object to the `Map()` constructor.
237+
238+
_Syntax:_
239+
`let map = new Map([iterable]);`
240+
241+
_Code:_
242+
243+
```js
244+
let fabThree = new Map([
245+
[kohli, "India"],
246+
[smith, "Australia"],
247+
[root, "England"],
248+
]);
249+
```
250+
251+
**Getting an element from a map by Key**
252+
253+
We can use the `get()` method to get an element from a map with key.
254+
255+
```js
256+
fabThree.get(smith); //OUTPUT: Australia
257+
```
258+
259+
If we somehow pass a key that does not exist, the `get()` method will return `undefined.`
260+
261+
```js
262+
fabThree.get(foo); //OUTPUT: undefined
263+
```
264+
265+
**Checking the existence of an element by Key**
266+
267+
We can use the `has()` method to check if the key exists in the map.
268+
269+
```js
270+
fabThree.has(bar); //OUTPUT: false
271+
fabThree.has(root); //OUTPUT: true
272+
```
273+
274+
**Get the numbers of elements in the map**
275+
276+
We can get the numbers of elements we have in the map with `size` property. The `size` property returns the number of elements in the Map object.
277+
278+
```js
279+
console.log(fabThree.size); //OUTPUT: 3
280+
```
281+
282+
**Iterate over map keys**
283+
284+
We can use the `keys()` method to obtain the keys of the `Map` object. The `keys()` method returns a new iterator object that contains the keys of the elements in the map (return value - array).
285+
286+
```js
287+
let virat = { fullName: "Virat Kohli" },
288+
smith = { fullName: "Steve Smith" },
289+
root = { fullName: "Joe Root" };
290+
291+
let fabThree = new Map([
292+
[virat, "India"],
293+
[smith, "Australia"],
294+
[root, "England"],
295+
]);
296+
297+
for (const player of fabThree.keys()) {
298+
console.log(player.fullName);
299+
}
300+
```
301+
302+
_OUTPUT:_
303+
304+
```
305+
Virat Kohli
306+
Steve Smith
307+
Joe Root
308+
```
309+
310+
**Iterate over Map values**
311+
312+
We can also iterate over values like with keys using `values()` method.
313+
314+
```js
315+
let virat = { fullName: "Virat Kohli" },
316+
smith = { fullName: "Steve Smith" },
317+
root = { fullName: "Joe Root" };
318+
319+
let fabThree = new Map([
320+
[virat, "India"],
321+
[smith, "Australia"],
322+
[root, "England"],
323+
]);
324+
325+
for (const player of fabThree.values()) {
326+
console.log(player);
327+
}
328+
```
329+
330+
_OUTPUT:_
331+
332+
```
333+
India
334+
Australia
335+
England
336+
```
337+
338+
**Iterate over entire map elements**
339+
340+
We van use the `entries()` method to iterate over map elements. The `entries()` method returns an iteratorobject that contains array of `[key, value]` of each element in that Map object.
341+
342+
```js
343+
let virat = { fullName: "Virat Kohli" },
344+
smith = { fullName: "Steve Smith" },
345+
root = { fullName: "Joe Root" };
346+
347+
let fabThree = new Map([
348+
[virat, "India"],
349+
[smith, "Australia"],
350+
[root, "England"],
351+
]);
352+
353+
for (let [player, country] of fabThree.entries()) {
354+
console.log(`${player.fullName}: ${country});
355+
}
356+
```
357+
358+
_OUTPUT:_
359+
360+
```
361+
Virat Kohli: India
362+
Steve Smith: Australia
363+
Joe Root: England
364+
```
365+
366+
We can also use `forEach()` instead of `for...of` loop.
367+
368+
```js
369+
fabThree.foreach((player, country) => {
370+
console.log(`${player.fullName}: ${country}`);
371+
});
372+
```
373+
374+
**Delete an element by Key**
375+
376+
We can use the `delete()` method to delete an entry in the map.
377+
378+
```
379+
fabThree.delete(root);
380+
```
381+
382+
**Delete all elements in the map**
383+
384+
We can clear all the entries in the Map object with the `clear()` method.
385+
386+
```
387+
fabThree.clear();
388+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /