In ES6, how do we quickly get the element?
in MDN Syntax for Set, I didn't find an answer for it.
In ES6, how do we quickly get the element?
in MDN Syntax for Set, I didn't find an answer for it.
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
theset.has(x)
. What you can't do in a set is retrieve an item by index, since sets are unordered. What javascript is missing is a way to retrieve any item from a set easily. That's what this answer is trying to provide. –
Scleroderma n
. –
Canso [...theSet][0]
–
Helga Set.prototype.first = function(){return this.values().next().value;};
after this: anyset.first()
will give you the first element –
Soane const [first] = theSet;
–
Mussorgsky first = set.values().next().value
. Method 2: first = [...set][0]
. Method 3: [first] = set
. Methods 1 and 3 are basically O(1) (about 15 million iterations per second in chrome, 1.5 million in firefox regardless of set size). Method 2 is O(n), scaling with n along a ratio at roughly 1s / 300'000'000 for chrome and 10 times slower for firefox. Don't use Method 2 (first = [...set][0]
) unless your sets are very very small: it's much costlier than the other methods. –
Lema O(1)
delete operation. Getting item at index n
would quite likely have O(n)
time complexity. –
Alow for...of
loopconst 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, typeof first);
// first item = 2 number
...spread
operatorconst 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];
if
in the for
loop? –
Alow let s = new Set([2,3,4])
let [head] = s
console.log(head)
simplified from @rodo's version
functional programming languages use head for the first elements in collections, some will prefer first.
A more direct way to get the first element from a Set (works for arrays too):
const mySet = new Set(['foo', 'bar', 'baz'])
const [firstElement, secondElement, thirdElement] = mySet
console.log(thirdElement, firstElement, secondElement) // baz foo bar
any
. –
Ehrman The most elegant and practical solution I have found is by retrieving the first IteratorResult and acquiring its value set.values().next().value
.
const set = new Set([0, 1, 2, 3, 4]);
const first = set.values().next().value; // first will be equal to 0
most simple way:
const data=new Set(['1', '2', '3']);
const first = [...data][0];
Using spread syntax (...):
const mySet = new Set([1, 2, 3, 4, 5]);
const firstElement = [...mySet][0];
console.log(firstElement); // Output: 1
Use destructuring
const set = new Set([1, 2, 3]);
const [first] = set;
console.log(first); // Output: 1
const set = new Set([1, 2, 3]);
const [, second] = set;
console.log(second); // 2
Using Set.values():
const mySet = new Set([1, 2, 3, 4, 5]);
const firstElement = mySet.values().next().value;
console.log(firstElement); // Output: 1
Using for...of loop:
const mySet = new Set([1, 2, 3, 4, 5]);
let firstElement;
for (const element of mySet) {
firstElement = element;
break;
}
console.log(firstElement); // Output: 1
Another choice will be to use the for..of
loop like so:
const mySet = new Set(["one", "two", 3]);
for (let item of mySet) {
console.log(item);
break;
}
The best way is to use the iterator on the Set<T>
:
const mySet = new Set<string>(['one', 'two', 'three']);
const firstValue = mySet.values().next().value;
console.log(firstValue);
You could also covert it to an array and then use normal indexing:
const mySet = new Set<string>(['one', 'two', 'three']);
const firstValue = Array.from(mySet)[0];
console.log(firstValue);
The first way is better from a complexity standpoint because it doesn't have to create a new array.
© 2022 - 2024 — McMap. All rights reserved.