Map and Set Flashcards

1
Q

_____are used for storing keyed collections.

______are used for storing ordered collections.

A

Objects
Arrays

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

Map is a collection of keyed data items, just like an Object. But the main difference is that Map allows_______

A

keys of any type.

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

unlike objects, keys are not converted to ______

A

unlike objects, keys are not converted to strings

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

creates the map

A

new Map()

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

The method sets and stores the value by the key.

A

map.set(key, value)

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

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

A

map.get(key) –

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

returns true if the key exists, false otherwise.

A

map.has(key)

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

removes the element (the key/value pair) by the key.

A

map.delete(key) –

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

removes everything from the map

A

map.clear()

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

returns the current element count.

A

map.size

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

Map can also use objects as _____

A

keys

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

let john = { name: “John” };

let visitsCountMap = new Map();

set map to return below

alert( visitsCountMap.get(john) ); // 123

A

visitsCountMap.set(john, 123);

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

For looping over a map, there are 3 methods:

A

map.keys() – returns an iterable for keys,

map.values() – returns an iterable for values,

map.entries() – returns an iterable for entries [key, value], it’s used by default in for..of.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

let recipeMap = new Map([
[‘cucumber’, 500],
[‘tomatoes’, 350],
[‘onion’, 50]
]);

// iterate over keys (vegetables)

A

for (let vegetable of recipeMap.keys()) {
alert(vegetable); // cucumber, tomatoes, onion
}

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

let recipeMap = new Map([
[‘cucumber’, 500],
[‘tomatoes’, 350],
[‘onion’, 50]
]);

// iterate over values (amounts)

A

for (let amount of recipeMap.values()) {
alert(amount); // 500, 350, 50
}

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

let recipeMap = new Map([
[‘cucumber’, 500],
[‘tomatoes’, 350],
[‘onion’, 50]
]);

// iterate over [key, value] entries

A

for (let entry of recipeMap) { // the same as of recipeMap.entries()
alert(entry); // cucumber,500 (and so on)
}

17
Q

The iteration goes in the same order as the values were inserted. Map preserves this order, unlike a regular Object.

true or false

A

true

18
Q

let map = new Map([
[‘1’, ‘str1’],
[1, ‘num1’],
[true, ‘bool1’]
]);

alert( map.get(‘1’) ); //

A

str1

19
Q

let map = new Map([
[‘1’, ‘str1’],
[1, ‘num1’],
[true, ‘bool1’]
]);

alert( ________ ); // true
what code to write?

A

map.get(true)

20
Q

let obj = {
name: “John”,
age: 30
};

let map = new Map(Object.entries(obj));

alert( map.get(‘name’) ); //

A

John

21
Q

let obj = {
name: “John”,
age: 30
};

let map =

alert( map.get(‘name’) ); // John

A

new Map(Object.entries(obj));

22
Q

_____________ method that does the reverse: given an array of [key, value] pairs, it creates an object from them:

A

Object.fromEntries

23
Q

let prices = Object.fromEntries([
[‘banana’, 1],
[‘orange’, 2],
[‘meat’, 4]
]);

// now prices =

A

{ banana: 1, orange: 2, meat: 4 }

24
Q

let prices = Object.fromEntries([
[‘banana’, 1],
[‘orange’, 2],
[‘meat’, 4]
]);

// now prices = { banana: 1, orange: 2, meat: 4 }

alert(prices.orange); //

A

2

25
Q

let set = // create new empty set

A

new Set()

26
Q

let set = new Set();

let john = { name: “John” };
let pete = { name: “Pete” };
let mary = { name: “Mary” };

// visits, some users come multiple times
set.add(john);
set.add(pete);
set.add(mary);
set.add(john);
set.add(mary);

// set keeps only unique values
alert( set.size ); // returns and why

A

3

keeps only unique values

27
Q

______– adds a value, returns the set itself.

A

set.add(value)

28
Q

__________– removes the value, returns true if value existed at the moment of the call, otherwise false.

A

set.delete(value)

29
Q

___________– returns true if the value exists in the set, otherwise false.

A

set.has(value)

30
Q

______________ removes everything from the set.

A

set.clear() –

31
Q

__________ – is the elements count.

A

set.size

32
Q

We can loop over a set either with

A

for..of
forEach:

33
Q

let set = new Set();

let john = { name: “John” };
let pete = { name: “Pete” };
let mary = { name: “Mary” };

set.add(john);
set.add(pete);
set.add(mary);

for (let user of set) {
alert(user.name); //
}

A

John (then Pete and Mary)

34
Q

let set = new Set();

let john = { name: “John” };
let pete = { name: “Pete” };
let mary = { name: “Mary” };

set.add(john);
set.add(pete);
set.add(mary);

A