Map and Set Flashcards

1
Q

What is a map object?

A
An object with keyed data items but can take keys of any type.
Strings
Numbers
Boolean
Objects
Null
Undefined
Infinity
NaN
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Create a new map

A

new Map()

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

Store a value by the key in a map object

A

Map.prototype.set(key, value)

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

Return the value by the key in a map object, undefined if key doesn’t exist in map.

A

Map.prototype.get(key)

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

Return true if the key exists in a map object, false otherwise.

A

Map.prototype.has(key)

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

Removes the value by the key in a map object

A

Map.prototype.delete(key)

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

Remove everything from the map object and return undefined

A

Map.prototype.clear()

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

Returns the current element count in a map object

A

Map.prototype.size

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
What does:
new Map()
do and/or return?
A

Create a new map

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

What does:
Map.prototype.set()
do and/or return?

A

Store a value by the key in a map object
Map.prototype.set(key, value)
Returns the map itself (can chain)

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

What does:
Map.prototype.get(key)
do and/or return?

A

Return the value by the key in a map object, undefined if key doesn’t exist in map.

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

What does:
Map.prototype.has(key)
do and/or return?

A

Return true if the key exists in a map object, false otherwise.

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

What does:
Map.prototype.delete(key)
do and/or return?

A

Removes the value by the key in a map object

Returns true or false based on whether the key existed or not.

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

What does:
Map.prototype.clear()
do and/or return?

A

Remove everything from the map object

Returns undefined

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

What does:
Map.prototype.size
do and/or return?

A

Returns the current element count in a map object

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

Does:
map[key]
map.key
work with map objects?

A

Yes, and they will behave like regular objects in this way

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

Returns iterables of keys, entries or values of a map objects

A

Map.prototype.keys()
Map.prototype.values()
Map.prototype.entries() (it’s used by default in for..of.
)

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

What order do map objects iterate over keys, values and entries?

A

The order they were added.

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

What does:
Map.prototype.forEach()
do and/or return?

A

.forEach does a function for each value in the map object

returns undefined

20
Q

Do a function for each item in the map object

2 ways

A

Map.prototype.forEach()

or use:
Map.prototype.keys()
Map.prototype.values()
Map.prototype.entries()
or the map itself (which is the same as .entries())
and a for of loop
21
Q

Create a new map objects with an array passed as an arugment to initiate some key/value pairs

A
let map = new Map([
  ['1',  'str1'],
  [1,    'num1'],
  [true, 'bool1']
]);
22
Q

Create a map object from a regular objects

A

Object.entries(obj) returns an array of the objects entries
Map objects can take an array at initialization to create some initial key/value pairs.

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

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

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

23
Q

Create a regular object from a map (2 ways, similar. 1 better because shorter)

A

Object.fromEntries() can take an iterable object and create an object.

map.entries() can create an iterable object from the map object’s entries.(though map objects are already iterable)

So:
let someMaps= new Map();
someMaps.set('banana', 1);
someMaps.set('orange', 2);
someMaps.set('meat', 4);

let obj = Object.fromEntries(someMaps.entries());

But:
let obj = Object.fromEntries(map); // omit .entries()
Because Object.fromEntries expects an iterable object as the argument. Not necessarily an array. And the standard iteration for map returns same key/value pairs as map.entries(). So we get a plain object with same key/values as the map.
24
Q

What is a set

A

a special type of collection without keys. just sets of values. E

25
Q

a set can have multiples of the same value

t or f

A

false

26
Q

create a Set object

A
new Set(iterable)
copies any iterable objects values into the set
27
Q

What does:
Set.prototype.add()
do and/or return?

A

adds a value,

returns the set itself.

28
Q

What does:
Set.prototype.delete()
do and/or return?

A

removes the value,

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

29
Q

What does:
Set.prototype.has()
do and/or return?

A

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

30
Q

What does:
Set.prototype.clear()
do and/or return?

A

removes everything from the set, returns undefined

31
Q

What does:
Set.prototype.size
do and/or return?

A

returns the elements count as a number

32
Q

What is a set object good for?

A

a way of ensuring multiple versions of the same value are not added in an efficient way.

33
Q

Iterate over a set object (2 ways)

A

for of
for (let value of set) alert(value);

Set.prototype.forEach()
set.forEach((value, valueAgain, set) => {
alert(value);
});

34
Q

Why does
Set.prototype.forEach()

have 2 arguments for the same value?
set.forEach((value, valueAgain, set) => {
alert(value);
});

A

To give it more compatibility with a map objects forEach method

Instead of having a different number of arguments. This makes it potentially easier to replace all maps with sets

35
Q

Return keys, values or entries as iterables from a set object

A

set. keys() – returns an iterable object for values,
set. values() – same as set.keys(), for compatibility with Map,
set. entries() – returns an iterable object for entries [value, value], exists for compatibility with Map.

36
Q

set objects are array like
t or f
set objects are iterable
t or f

A

True

True

37
Q

Create a function unique(arr) that should return an array with unique items of arr.

function unique(arr) {
  /* your code */
}

let values = [“Hare”, “Krishna”, “Hare”, “Krishna”,
“Krishna”, “Krishna”, “Hare”, “Hare”, “:-O”
];

alert( unique(values) ); // Hare, Krishna, :-O

A

alert( unique(values) );

38
Q

map objects are array like
t or f
map objects are iterable
t or f

A

True

True

39
Q

Create an array from a set or map

A

Array.from(set or map object))

40
Q
What happens to an Object key when you reassign a Map variable? 
let john = { name: "John" };
let myMap = new Map();
map.set(john, "...");

john = null;

Can you still get the john object?

A

Yes, you can get it using myMap.keys()

41
Q
What happens to an Object key when you reassign a weakMap variable? 
let john = { name: "John" };
let myMap = new Map();
map.set(john, "...");

john = null;

Can you still get the john object?

A

No

Everything gets wiped

42
Q

What are some differences between Map() and WeakMap() objects? (3 differences)

A
  • WeakMap objects delete the object keys when the object used as the key is removed
  • WeakMap has to have objects as keys. Map can use any value type as a key
  • WeakMap does not take iteration methods like keys(), values, or entries.
    It only has:
    weakMap.get(key)
    weakMap.set(key, value)

weakMap.delete(key)
weakMap.has(key)

43
Q

Name all WeakMap methods

Why so few?

A

weakMap.get(key)
weakMap.set(key, value)
weakMap.delete(key)
weakMap.has(key)

The JavaScript engine decides that. It may choose to perform the memory cleanup immediately or to wait and do the cleaning later when more deletions happen. So, technically, the current element count of a WeakMap is not known. The engine may have cleaned it up or not, or did it partially. For that reason, methods that access all keys/values are not supported.

44
Q

What is WeakMap good for?

2 uses

A
  1. The main area of application for WeakMap is an additional data storage.

If we’re working with an object that “belongs” to another code, maybe even a third-party library, and would like to store some data associated with it, that should only exist while the object is alive – then WeakMap is exactly what’s needed.

We put the data to a WeakMap, using the object as the key, and when the object is garbage collected, that data will automatically disappear as well.

A Map() object would have to be manually cleaned once in a while.

  1. Additionally it can be used for cach
45
Q

What is a WeakSet object?

A

A set that only allows objects that deletes them when the object is removed.

A regular Set() object will keep them stored?

46
Q

What methods are on WeakSet()

A

it supports add, has and delete,

but not size, keys() and no iterations.

47
Q

What is weakset used for?

A

Being “weak”, it also serves as additional storage. But not for arbitrary data, rather for “yes/no” facts. A membership in WeakSet may mean something about the object.

For instance, we can add users to WeakSet to keep track of those who visited our site: