Map Flashcards
The Map object holds key-value pairs. Any value (both objects and primitive values) may be used as either a key or a value.
Returns the number of elements in a Map object.
var map1 = new Map();
map1. set(‘a’, ‘alpha’);
map1. set(‘b’, ‘beta’);
map1. set(‘g’, ‘gamma’);
console.log(map1.size); // expected output: 3
Method removes all elements from a Map object.
var map1 = new Map();
map1. set(‘bar’, ‘baz’);
map1. set(1, ‘foo’);
console.log(map1.size); // expected output: 2
map1.clear();
console.log(map1.size); // expected output: 0
Method removes the specified element from a Map object.
var map1 = new Map(); map1.set('bar', 'foo');
console.log(map1.delete('bar')); // expected result: true // (true indicates successful removal)
console.log(map1.has('bar')); // expected result: false
Method returns a new Iterator object that contains the [key, value] pairs for each element in the Map object in insertion order.
var map1 = new Map();
map1. set(‘0’, ‘foo’);
map1. set(1, ‘bar’);
var iterator1 = map1.entries();
console.log(iterator1.next().value); // expected output: ["0", "foo"]
console.log(iterator1.next().value); // expected output: [1, "bar"]
Method executes a provided function once per each key/value pair in the Map object, in insertion order.
function logMapElements(value, key, map) { console.log(`m[${key}] = ${value}`); }
new Map([['foo', 3], ['bar', {}], ['baz', undefined]]) .forEach(logMapElements);
// expected output: "m[foo] = 3" // expected output: "m[bar] = [object Object]" // expected output: "m[baz] = undefined"
Method returns a specified element from a Map object.
var map1 = new Map(); map1.set('bar', 'foo');
console.log(map1.get('bar')); // expected output: "foo"
console.log(map1.get('baz')); // expected output: undefined
Method returns a boolean indicating whether an element with the specified key exists or not.
var map1 = new Map(); map1.set('bar', 'foo');
console.log(map1.has('bar')); // expected output: true
console.log(map1.has('baz')); // expected output: false
Method returns a new Iterator object that contains the keys for each element in the Map object in insertion order.
var map1 = new Map();
map1. set(‘0’, ‘foo’);
map1. set(1, ‘bar’);
var iterator1 = map1.keys();
console.log(iterator1.next().value); // expected output: "0"
console.log(iterator1.next().value); // expected output: 1
Method adds or updates an element with a specified key and value to a Map object.
var map1 = new Map(); map1.set('bar', 'foo');
console.log(map1.get('bar')); // expected output: "foo"
console.log(map1.get('baz')); // expected output: undefined
Method returns a new Iterator object that contains the values for each element in the Map object in insertion order.
var map1 = new Map();
map1. set(‘0’, ‘foo’);
map1. set(1, ‘bar’);
var iterator1 = map1.values();
console.log(iterator1.next().value); // expected output: "foo"
console.log(iterator1.next().value); // expected output: "bar"