Maps Flashcards

1
Q

An Object has a prototype, so there are default keys in the map. However, this can be bypassed using map = __________.

A

Object.create(null)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

The keys of an objects are __________, where they can be any value for a Map.

A

strings

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Maps can be iterated using a

A

for..of loop:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

ITERATE THROUGH THIS MAP for of

var myMap = new Map();
myMap.set(0, ‘zero’);
myMap.set(1, ‘one’);

// CODE HERE

// 0 = zero
// 1 = one
A

for (var [key, value] of myMap) {
console.log(key + ‘ = ‘ + value);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

ITERATE THROUGH THIS MAP for of

var myMap = new Map();
myMap.set(0, ‘zero’);
myMap.set(1, ‘one’);

// CODE HERE

// 0
// 1
A

for (var key of myMap.keys()) {
console.log(key);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

ITERATE THROUGH THIS MAP for of

var myMap = new Map();
myMap.set(0, ‘zero’);
myMap.set(1, ‘one’);

// CODE HERE

// zero
// one
A

for (var value of myMap.values()) {
console.log(value);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

ITERATE THROUGH THIS MAP forEach

var myMap = new Map();
myMap.set(0, ‘zero’);
myMap.set(1, ‘one’);

// CODE HERE

// Will show 2 logs; first with “0 = zero” and second with “1 = one”

A

myMap.forEach(function(value, key) {
console.log(key + ‘ = ‘ + value);
});

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

var original = new Map([
[1, ‘one’]
]);

var clone = new Map(original);

console. log(clone.get(1)); //
console. log(original === clone); //

A

one

false. Useful for shallow comparison

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

let ages = new Map();

SET key to “Boris” and “Value” to 39

A

ages.set(“Boris”, 39);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

____________are special objects per se, they are iterables with key value pair constructor that looks like a 2D array but acts like an object. They offer a better flexibility in terms of choosing our key values. A ______ can have a key value which can be a string, number, object or even NaN.

A

Maps

Maps

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

The _________method removes all elements from a Set object.

A

clear()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

in a ____ you can not have 2 of the same items

A

set

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
convert a set to array 
const myArray =
A

array.from(name of set)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

var myMap = new Map();
myMap.set(‘0’, ‘foo’);
myMap.set(1, ‘bar’);
myMap.set({}, ‘baz’);

var mapIter = myMap.keys();

console. log(________________); // “0”
console. log(_________________); // 1
console. log(_________________; // Object

A

mapIter.next().value
mapIter.next().value
mapIter.next().value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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

A

forEach()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

This iterates over a list and applies some operation with side effects to each list member (example: saving every list item to the database)

A

forEach()

17
Q

This iterates over a list, transforms each member of that list, and returns another list of the same size with the transformed members (example: transforming list of strings to uppercase)

A

map

18
Q

The _____ method returns a specified element from a Map object.

A

get()

19
Q

_______ – creates the map.

A

new Map()

20
Q

________ – stores the value by the key.

A

map.set(key, value)

21
Q

________returns the value by the key, undefined if key doesn’t exist in map.

A

map.get(key) –

22
Q

__________– returns true if the key exists, false otherwise.

A

map.has(key)

23
Q

_________– removes the value by the key.

A

map.delete(key) – removes the value by the key.

24
Q

_________– clears the map

A

map.clear()

25
Q

_________ – returns the current element count.

A

map.size

26
Q

“chain” the calls:

let map = new Map();

map. set(‘1’, ‘str1’);
map. set(1, ‘num1’);
map. set(true, ‘bool1’);

A

map.set(‘1’, ‘str1’)
.set(1, ‘num1’)
.set(true, ‘bool1’);

27
Q

For looping over a map, there are 3 methods:

A

map. keys()
map. values()
map. entries()

28
Q

map.entries() – returns an iterable for entries [key, value], it’s used by default in __________

A

for..of.

29
Q

_________ – returns an iterable for entries [key, value], it’s used by default in for..of.

A

map.entries()

30
Q
let recipeMap = new Map([
  ['cucumber', 500],
  ['tomatoes', 350],
  ['onion',    50]
]);

for (let vegetable of recipeMap.keys()) {
alert(vegetable); //
}

A

cucumber, tomatoes, onion

31
Q
let recipeMap = new Map([
  ['cucumber', 500],
  ['tomatoes', 350],
  ['onion',    50]
]);

for (let vegetable of recipeMap.values()) {
alert(vegetable); //
}

A

500, 350, 50