Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are keys and values the same in ES6 set?

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 ?

like image 895
Alexander Solonik Avatar asked Oct 17 '22 03:10

Alexander Solonik


1 Answers

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.

like image 71
Jonas Wilms Avatar answered Oct 21 '22 00:10

Jonas Wilms