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.

1
Q

Returns the number of elements in a Map object.

A

var map1 = new Map();

map1. set(‘a’, ‘alpha’);
map1. set(‘b’, ‘beta’);
map1. set(‘g’, ‘gamma’);

console.log(map1.size);
// expected output: 3
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Method removes all elements from a Map object.

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Method removes the specified element from a Map object.

A
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Method returns a new Iterator object that contains the [key, value] pairs for each element in the Map object in insertion order.

A

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"]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Method executes a provided function once per each key/value pair in the Map object, in insertion order.

A
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"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Method returns a specified element from a Map object.

A
var map1 = new Map();
map1.set('bar', 'foo');
console.log(map1.get('bar'));
// expected output: "foo"
console.log(map1.get('baz'));
// expected output: undefined
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Method returns a boolean indicating whether an element with the specified key exists or not.

A
var map1 = new Map();
map1.set('bar', 'foo');
console.log(map1.has('bar'));
// expected output: true
console.log(map1.has('baz'));
// expected output: false
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Method returns a new Iterator object that contains the keys for each element in the Map object in insertion order.

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Method adds or updates an element with a specified key and value to a Map object.

A
var map1 = new Map();
map1.set('bar', 'foo');
console.log(map1.get('bar'));
// expected output: "foo"
console.log(map1.get('baz'));
// expected output: undefined
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Method returns a new Iterator object that contains the values for each element in the Map object in insertion order.

A

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"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly