They don't seem to expose the List to be accesible from the instanced Object. This is from the EcmaScript Draft:
23.2.4 Properties of Set Instances
Set instances are ordinary objects that inherit properties from the Set prototype. Set instances also have a [[SetData]] internal slot.
[[SetData]] is the list of Values the Set is holding.
A possible solution (an a somewhat expensive one) is to grab an iterator and then call next() for the first value:
var x = new Set();
x.add(1);
x.add({ a: 2 });
//get iterator:
var it = x.values();
//get first entry:
var first = it.next();
//get value out of the iterator entry:
var value = first.value;
console.log(value); //1
const set = new Set();
set.add(2);
set.add(3);
// return the first item of Set ✅
function getFirstItemOfSet(set) {
for(let item of set) {
if(item) {
return item;
}
}
return undefined;
}
const first = getFirstItemOfSet(set);
console.log('first item =', first);
destructuring assignment
const set = new Set();
set.add(2);
set.add(3);
// only get the first item ✅
const [first] = set;
console.log('first item =', first);
...spread operator
const set = new Set();
set.add(2);
set.add(3);
// convert Set to Array ✅
const first = [...set][0];
console.log('first item =', first);
iterator & next()
const set = new Set();
set.add(2);
set.add(3);
// iterator ✅
const first = set.keys().next().value;
console.log(`first item =`, first);
// OR
set.values().next().value;
// OR
set.entries().next().value[0];
// OR
set.entries().next().value[1];