Index
Coding a Map Using Objects


In this lab we'll play with JavaScript's newer Map type.

A map (or dictionary) is an key-value lookup. Like a word dictionary. Given a key we look up a value. A key is associated with a value.

Constructing and initializing a map using a simple object

//Initialize map
const map = new Map([['width', 1000], ['pickle', 2000],
					['fun', 3000], ['big', 4000]]);
Getting values from map (object)

let obj;
//Initialize map
const map = new Map([['width', 1000], ['pickle', 2000],
					['fun', 3000], ['big', 4000]]);

//Now we "get" the value at the key
prn(map.get('width'));
prn(map.get('pickle'));
Setting values into map (object)

let map;

//Now we "set" key-value pairs into map

//Two styles (similar to "gettingValues")

//Set values into map
map = new Map();
map.set('width', 333);
map.set('height', 444);
prn(map.get('width'));
prn(map.get('height'));
Iterating over the map

//Initialize map
const map = new Map([['width', 1000], ['pickle', 2000],
					['fun', 3000], ['big', 4000]]);

//Iterate
for (const keyValue of map){
	prn(keyValue);
}
//Iterate (extracting from pair)
for (const keyValue of map){
	prn(`key is ${keyValue[0]}, value is ${keyValue[1]}`);
}

More Example Sources


Here are a couple (among many more) online related example sources: