maps and sets Flashcards
How can i create a set? and what happens if i pass a string into a set (not nested in array)
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 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?
console. log(nameSet.size)
console. log(nameSet.has(‘item’))
console. log(nameSet.add(‘newItem’))
console. log(nameSet.delete(‘item’)
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?
No, if i need to retrieve, use an array.
console.log(nameSet.clear())
- when should i use a set?
2. How can I convert an array with duplicate values to an array w/ unique values?
- Use sets to remove duplicate values in arrays
- convert array into a set and then wrap it in a spread operator
const staffUnique = […new Set(staffArrayWithDuplicates)];
How can I declare a new map?
const myMap = new Map();
How can I set key/value pairs in a map?
console.log( myMap.set(key, ‘values’ ) )
How can I easily set key/values to an empty map?
yMap.set( key, value).set( key/value).set( key/value)
How can I retrieve values from a map?
console(myMap.get( key) )
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
object.get(if time > obj.get(‘open’) && time < obj.get(‘close’) (the bool keys are intuitive!!!!)
How do I see if a key exists in a map?
console(myMap.has(‘keyName’))
How can i see the amount of items in a map?
console(myMap.size)
Can i implement an array as a map key? If so, how?
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 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?
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 can I create an array of unique values, taken from a map (with duplicate values)?
const myArray = [...new Set(mapName.values())]; console.log(myArray)
how can i loop a map?
for (const [key, value] of gameEvents)