MJS: Map, Set, Weakmap, and Weakset

MJS: Map, Set, Weakmap, and Weakset

memorize.aimemorize.ai (lvl 286)
Section 1

Preview this deck

What does map.get do?

Front

Star 0%
Star 0%
Star 0%
Star 0%
Star 0%

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Active users

0

All-time users

0

Favorites

0

Last updated

6 years ago

Date created

Mar 14, 2020

Cards (39)

Section 1

(39 cards)

What does map.get do?

Front

map.get(key) - returns the value by the key, undefined if key doesn't exist in map.

Back

Can primitives be used as keys in WeakMap/weakSet?

Front

No, keys must be objects

Back

What is the main use of set?

Front

Storing unique values

Back

What does map.set do?

Front

map.set(key, value) - stores the value by the key.

Back

How does the insertion order differ in map objects as opposed to normal objects?

Front

The insertion order is preserved in maps, and not preserved in normal objects.

Back

What are WeakMap and WeakSet?

Front

WeakSet is a special kind of Set that does not prevent JavaScript from removing its items from memory. WeakMap is the same thing for Map. WeakMap/WeakSet does not prevent the object removal from the memory.

Back

What is the syntax for map's forEach method?

Front

recipeMap.forEach( (value, key, map) => { alert(`${key}: ${value}`); // cucumber: 500 etc });

Back

What are the iterators for set?

Front

The same methods as Map has for iterators are also supported: 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.

Back

What is the reasoning behind methods not being able to access weakMap/weakSet as a whole?

Front

The javascript engine may or may not have removed objects keys, so we don't know how many elements weakMap has.

Back

What are the main methods of set?

Front

new Set(iterable) set.add(value) set.delete(value) set.has(value) set.clear() set.size

Back

What does set.add do?

Front

- adds a value, returns the set itself.

Back

What are the 3 methods for iterating over map?

Front

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

Back

How does Map compare keys?

Front

it uses the algorithm SameValueZero, which isn't exactly ===. NaN == NaN for this algo.

Back

Can you chain calls on a single map?

Front

Yes, as the calls return the map itself which pushes it up to the next call.

Back

What are the main methods of Map?

Front

new Map() map.set(key, value) map.get(key) map.has(key) map.delete(key) map.clear() map.size

Back

What does map.delete do?

Front

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

Back

How do you convert a general object into a map?

Front

Use the built-in method Object.entries when initializing a map. let map = new Map(Object.entries({ name: "John", age: 30 })); Here, Object.entries returns the array of key/value pairs: [ ["name","John"], ["age", 30] ]. That's what Map needs.

Back

What does set.delete do?

Front

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

Back

Can weakMap/weakSet be iterated over?

Front

No, it has no iterator methods

Back

What does map.keys do?

Front

- returns an iterable for keys,

Back

What is the idea behind using weakMap/weakSet?

Front

The idea of WeakMap is that we can store something for an object that exists only while the object exists. But we do not force the object to live by the mere fact that we store something for it. weakMap.put(john, "secret documents"); // if john dies, secret documents will be destroyed

Back

Why does the forEach for set have 2 value arguments that are the same thing?

Front

That's made for compatibility with Map where forEach has three arguments.

Back

What does set.has do?

Front

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

Back

What does new Map() do?

Front

new Map() - creates the map.

Back

What does map.has do?

Front

map.has(key) - returns true if the key exists, false otherwise.

Back

What is Set?

Front

Set - is a collection of values, where each value may occur only once.

Back

What does map.entries do?

Front

- returns an iterable for entries [key, value], it's used by default in for..of.

Back

Which methods does weakMap/weakSet have?

Front

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

Back

What does map.size do?

Front

map.size - is the current elements count.

Back

What does map.clear do?

Front

map.clear() - clears the map

Back

What is the forEach syntax for set?

Front

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

Back

What does new Set do?

Front

- creates the set, optionally from an array of values (any iterable will do).

Back

What is Map? How is it better than a less complex version of whatever it is?

Front

Map is a collection of keyed data items. Just like an Object. But the main difference is that Map allows keys of any type.

Back

t/f: maps can use objects as keys

Front

True

Back

What is the syntax for using map iterables?

Front

let recipeMap = new Map([ ['cucumber', 500], ['tomatoes', 350], ['onion', 50] ]); // iterate over keys (vegetables) for(let vegetable of recipeMap.keys()) { alert(vegetable); // cucumber, tomateos, onion } // iterate over values (amounts) for(let amount of recipeMap.values()) { alert(amount); // 500, 350, 50 } // iterate over [key, value] entries for(let entry of recipeMap) { // the same as of recipeMap.entries() alert(entry); // cucumber,500 (and so on) }

Back

What does set.clear do?

Front

- removes everything from the set.

Back

What does map.values do?

Front

- returns an iterable for values,

Back

What does set.size do?

Front

- is the elements count.

Back

How do you loop over set?

Front

Use the foreach syntax

Back