GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing zetcode.com — JavaScript Map - Key/Value Collection Explained JavaScript Maplast modified last modified October 18, 2023 In this article we show how to work with a Map collection in JavaScript. Map
A
We can use the JS Map methodsThe following methods are used to work with a map:
Also, the JS Map simple exampleThe following is a simple example using Map. simple.js
let stones = new Map(); stones.set(0, "citrine"); stones.set(1, "garnet"); stones.set(2, "topaz"); stones.set(3, "opal"); stones.set(4, "amethyst"); console.log(stones); console.log(stones.get(0)); console.log(stones.get(3)); console.log(stones.get(9)); We have a map of stones. The keys are integers and the values are strings. let stones = new Map(); An empty map is created. stones.set(0, "citrine");
A new key/value pair is inserted with console.log(stones.get(0)); We get the value which has key equal to 0. $ node simple.js
Map {
0 => 'citrine',
1 => 'garnet',
2 => 'topaz',
3 => 'opal',
4 => 'amethyst'
}
citrine
opal
undefined
If the value is not present, the JS initiating MapThere are several ways to initiate an Map. initiate.js
let stones = new Map(); stones.set(0, "citrine"); stones.set(1, "garnet"); stones.set(2, "topaz"); let stones2 = new Map(stones); console.log(stones); console.log(stones2); let items = new Map([["coin", 3], ["pen", 4], ["cup", 3]]); console.log(items); The example creates a new map from an empty map, from an existing map, and from an array. $ node initiate.js
Map { 0 => 'citrine', 1 => 'garnet', 2 => 'topaz' }
Map { 0 => 'citrine', 1 => 'garnet', 2 => 'topaz' }
Map { 'coin' => 3, 'pen' => 4, 'cup' => 3 }
JS Map size
The size of the Map is determined with the map_size.js
let stones = new Map();
console.log(`The size is ${stones.size}`);
stones.set(0, "citrine");
stones.set(1, "garnet");
stones.set(2, "topaz");
console.log(`The size is ${stones.size}`);
stones.set(3, "opal");
stones.set(4, "amethyst");
console.log(`The size is ${stones.size}`);
stones.clear();
console.log(`The size is ${stones.size}`);
We initiate a new empty map, add new pairs and determine the current size
with the $ node map_size.js The size is 0 The size is 3 The size is 5 The size is 0 JS Map loop
We can easily loop over the key, values, and key/value pairs with
looping.js
let stones = new Map();
stones.set(0, "citrine");
stones.set(1, "garnet");
stones.set(2, "topaz");
stones.set(3, "opal");
stones.set(4, "amethyst");
for (const entry of stones)) {
console.log(entry);
}
console.log('-------------------------------');
for (const [k, v] of stones.entries()) {
console.log(`${k}: ${v}`);
}
console.log('-------------------------------');
for (const val of stones.values()) {
console.log(val);
}
console.log('-------------------------------');
for (const key of stones.keys()) {
console.log(key);
}
We define a new map and loop over it in various ways. $ node looping.js [ 0, 'citrine' ] [ 1, 'garnet' ] [ 2, 'topaz' ] [ 3, 'opal' ] [ 4, 'amethyst' ] ------------------------------- 0: citrine 1: garnet 2: topaz 3: opal 4: amethyst ------------------------------- citrine garnet topaz opal amethyst ------------------------------- 0 1 2 3 4
Another way to loop over a map is to use the looping2.js
let stones = new Map();
stones.set(0, "citrine");
stones.set(1, "garnet");
stones.set(2, "topaz");
stones.set(3, "opal");
stones.set(4, "amethyst");
stones.forEach((v, k) => {
console.log(`${k} has value ${v}`);
});
We loop over a map of stones with $node looping2.js 0 has value citrine 1 has value garnet 2 has value topaz 3 has value opal 4 has value amethyst JS Map transform into array
The array_from.js
let stones = new Map();
stones.set(0, "citrine");
stones.set(1, "garnet");
stones.set(2, "topaz");
stones.set(3, "opal");
stones.set(4, "amethyst");
let stones2d = Array.from(stones);
let keys = Array.from(stones.keys());
let values = Array.from(stones.values());
console.log(stones2d);
console.log([...stones]);
console.log('--------------------');
console.log(keys);
console.log(values);
In the example, we turn the map into arrays. let stones2d = Array.from(stones);
We create a 2D array from the let keys = Array.from(stones.keys()); We create an array of map keys. let values = Array.from(stones.values()); We create an array of map values. console.log([...stones]); Another way to create a 2D array is to use the spread operator. $ node array_from.js
[
[ 0, 'citrine' ],
[ 1, 'garnet' ],
[ 2, 'topaz' ],
[ 3, 'opal' ],
[ 4, 'amethyst' ]
]
[
[ 0, 'citrine' ],
[ 1, 'garnet' ],
[ 2, 'topaz' ],
[ 3, 'opal' ],
[ 4, 'amethyst' ]
]
--------------------
[ 0, 1, 2, 3, 4 ]
[ 'citrine', 'garnet', 'topaz', 'opal', 'amethyst' ]
JS Map combine
In the following example, we combine two maps. The maps are combined with the
spread combining.js
const assert = require('assert');
let stones1 = new Map();
stones1.set(0, "citrine");
stones1.set(1, "garnet");
stones1.set(2, "topaz");
let stones2 = new Map();
stones2.set(3, "opal");
stones2.set(4, "amethyst");
let stones = new Map([...stones1, ...stones2])
console.log(stones);
assert.deepStrictEqual(
[...stones],
[ [ 0, 'citrine' ],
[ 1, 'garnet' ],
[ 2, 'topaz' ],
[ 3, 'opal' ],
[ 4, 'amethyst' ] ]
);
We combine two maps and verify that the actual and expected values are equal.
The $ node combining.js
Map {
0 => 'citrine',
1 => 'garnet',
2 => 'topaz',
3 => 'opal',
4 => 'amethyst'
}
SourceIn this article we have worked with the maps in JavaScript. AuthorMy name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |