Sets Flashcards
const set1 = new Set();
set1. add(42);
set1. add(42);
set1. add(13);
for (let item of set1) { console.log(item); // expected output: // expected output:
42
13
const mySet = new set []
//add a number 99 to this set
mySet.add(99);
how to know size of a set
set.size()
to check if a set contains an elemet
set.has()
const mySet = new Set(['1', '2', '3']); [...mySet].indexOf('2') //
returns 1
_________have no built-in function to retrieve or find the index of its items even-though its an iterable, so ideally we would have to convert it to an array before indexOf/find operation.
sets
_______ – creates the set, optionally from an array of values (any iterable will do).
new Set(iterable)
_________ – adds a value, returns the set itself.
set.add(value)
__________– removes the value, returns true if value existed at the moment of the call, otherwise false.
set.delete(value)
________ – returns true if the value exists in the set, otherwise false.
set.has(value)
_________– removes everything from the set.
set.clear()
_________ – is the elements count.
set.size
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);
set. add(john);
set. add(mary);
alert( set.size ); //
3
duplicate names dont get returned.
We can loop over a set either with ______ or using ______
for…of
forEach
let set = new Set([“oranges”, “apples”, “bananas”]);
Iterate using for…of
for (let value of set) alert(value);