maps and sets Flashcards

1
Q

How can i create a set? and what happens if i pass a string into a set (not nested in array)

A

const orderSet = new Set(‘jonas’);

or as an array inside: orderSet = new Set([1,2,3,4,5])

if i pass a str in set(not inside an array) and print it, it will put each char in quotes

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

How can i find out how many items are in a set?

how can i find out if a specific item exists in a set?

How can I add items? How can i delete items?

A

console. log(nameSet.size)
console. log(nameSet.has(‘item’))
console. log(nameSet.add(‘newItem’))
console. log(nameSet.delete(‘item’)

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

Can i call index numbers like: nameSet[0]? Or can i retrieve data from a set?

how can I clear the entire contents of a Set?

A

No, if i need to retrieve, use an array.

console.log(nameSet.clear())

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
  1. when should i use a set?

2. How can I convert an array with duplicate values to an array w/ unique values?

A
  1. Use sets to remove duplicate values in arrays
  2. convert array into a set and then wrap it in a spread operator

const staffUnique = […new Set(staffArrayWithDuplicates)];

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

How can I declare a new map?

A

const myMap = new Map();

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

How can I set key/value pairs in a map?

A

console.log( myMap.set(key, ‘values’ ) )

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

How can I easily set key/values to an empty map?

A

yMap.set( key, value).set( key/value).set( key/value)

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

How can I retrieve values from a map?

A

console(myMap.get( key) )

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

I want to apply boolean values which are keys; conditionally, using the open and close values of other keys. how can I do this in prcatice? my variable is time

A

object.get(if time > obj.get(‘open’) && time < obj.get(‘close’) (the bool keys are intuitive!!!!)

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

How do I see if a key exists in a map?

A

console(myMap.has(‘keyName’))

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

How can i see the amount of items in a map?

A

console(myMap.size)

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

Can i implement an array as a map key? If so, how?

A

Yes. first, declare an array outside of the map. Then refer to that array in the key. All of the items inside of the array refer to the same value.

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

How can i populate an empty map with each key value pair represented as an array? . this is different from using the set method? Why is this way useful?

A

create a new map like before, but inside the evanders, set an array. then, within the array i will set a new array for each key/value pair. like this:
const question = new Map([
[‘question’, ‘whats the best language?’], [1, ‘c’];
useful if there are a ton of values!!

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

how can I create an array of unique values, taken from a map (with duplicate values)?

A
const myArray = [...new Set(mapName.values())];
console.log(myArray)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

how can i loop a map?

A

for (const [key, value] of gameEvents)

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