Wednesday, June 5, 2024
165
rated 0 times [  170] [ 5]  / answers: 1 / hits: 86629  / 9 Years ago, sat, september 12, 2015, 12:00:00

In ES6, how do we quickly get the element?



in MDN Syntax for Set, I didn't find an answer for it.


More From » ecmascript-6

 Answers
0

They don't seem to expose the List to be accessible 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 (and 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

Worth mentioning too:


Set.prototype.values === Set.prototype.keys

[#65092] Thursday, September 10, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
anais

Total Points: 672
Total Questions: 118
Total Answers: 121

Location: Oman
Member since Fri, Dec 23, 2022
1 Year ago
anais questions
Fri, Jul 29, 22, 00:00, 2 Years ago
Mon, Jul 19, 21, 00:00, 3 Years ago
Tue, May 11, 21, 00:00, 3 Years ago
Fri, Apr 2, 21, 00:00, 3 Years ago
;