I was just going through the MDN documentation for set , and how it works , coming to the part of how to iterate over a set , i saw the following examples:
// logs the items in the order: 1, "some text", {"a": 1, "b": 2}
for (let item of mySet) console.log(item);
And
// logs the items in the order: 1, "some text", {"a": 1, "b": 2}
for (let item of mySet.values()) console.log(item);
And
// logs the items in the order: 1, "some text", {"a": 1, "b": 2}
//(key and value are the same here)
for (let [key, value] of mySet.entries()) console.log(key);
Just to confirm , does this mean that when using set the keys and values are the same ?
The entries() method returns a new Iterator object that contains an array of [value, value] for each element in the Set object, in insertion order [...].
MDN docs
So no, Sets don't have keys at all, however .entries()
lets you believe so for having consistency between Maps and Sets.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With