Pure Programmer
Blue Matrix


Cluster Map

Maps (aka Associative Arrays)

L1

This page is under construction. Please come back later.

Maps1.js
#!/usr/bin/env node;
const Utils = require('./Utils');

const main = async () => {
	let dictionary = new Map();

	console.log("dictionary size: " + dictionary.size);
	console.log("dictionary is " + (Utils.isEmpty(dictionary) ? "empty" : "not empty"));
	dictionary.set("apple", "red");
	dictionary.set("banana", "yellow");
	dictionary.set("cantaloupe", "orange");
	dictionary.set("dragonfruit", "red");
	dictionary.set("elderberry", "purple");

	console.log("dictionary size: " + dictionary.size);
	console.log("dictionary is " + (Utils.isEmpty(dictionary) ? "empty" : "not empty"));

	console.log("bananas are " + dictionary.get("banana"));

	let x = "dragonfruit";
	if (dictionary.has(x)) {
		console.log(x + " are " + dictionary.get(x));
	} else {
		console.log("Can't find " + x);
	}

	x = "fig";
	if (dictionary.has(x)) {
		console.log(x + " are " + dictionary.get(x));
	} else {
		console.log("Can't find " + x);
	}

	x = "elderberry";
	dictionary.delete(x);
	if (dictionary.has(x)) {
		console.log(x + " are " + dictionary.get(x));
	} else {
		console.log("Can't find " + x);
	}

	console.log(Utils.mapToString(dictionary));
}
main().catch( e => { console.error(e) } );
Output
$ node Maps1.js dictionary size: 0 dictionary is not empty dictionary size: 5 dictionary is not empty bananas are yellow dragonfruit are red Can't find fig Can't find elderberry {"apple" => "red", "banana" => "yellow", "cantaloupe" => "orange", "dragonfruit" => "red"}
Maps2.js
#!/usr/bin/env node;
const Utils = require('./Utils');

const main = async () => {
	let planetDiameters = new Map([
		["Mercury", 4879],
		["Venus", 12103],
		["Earth", 12756],
		["Mars", 6794],
		["Jupiter", 142985],
		["Saturn", 120534],
		["Uranus", 51115],
		["Neptune", 49534],
		["Pluto", 2374],
		["Ceres", 946],
		["Eris", 2326],
		["Makemake", 1430]
	]);

	for (let planet of planetDiameters.keys()) {
		console.log(planet + " has a diameter of " + planetDiameters.get(planet) + " km");
	}
}
main().catch( e => { console.error(e) } );
Output
$ node Maps2.js Mercury has a diameter of 4879 km Venus has a diameter of 12103 km Earth has a diameter of 12756 km Mars has a diameter of 6794 km Jupiter has a diameter of 142985 km Saturn has a diameter of 120534 km Uranus has a diameter of 51115 km Neptune has a diameter of 49534 km Pluto has a diameter of 2374 km Ceres has a diameter of 946 km Eris has a diameter of 2326 km Makemake has a diameter of 1430 km
javascript

Questions

Projects

More ★'s indicate higher difficulty level.

References