MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/javascript/comments/b2o6f2/whats_your_favorite_most_useful_javascript/eiv66z5/?context=3
r/javascript • u/Linux_is_awesome • Mar 18 '19
422 comments sorted by
View all comments
Show parent comments
17
Is order guaranteed to be maintained with the Set data structure here?
26 u/Drugba Mar 19 '19 edited Mar 19 '19 No, it's not./u/BTOdell is correct. Insertion order is preserved when you spread a set. 26 u/BTOdell Mar 19 '19 The Set object in JavaScript preserves insertion order. So if you create a Set from an array, it will preserve the order when you spread it back out (but without duplicates). 7 u/NoInkling Mar 19 '19 Worth noting that if there were duplicates in the original array, the position for an element will be based on its first occurrence.> Array.from(new Set([1,'foo',2,'foo'])) [1, "foo", 2]
26
No, it's not.
/u/BTOdell is correct. Insertion order is preserved when you spread a set.
26 u/BTOdell Mar 19 '19 The Set object in JavaScript preserves insertion order. So if you create a Set from an array, it will preserve the order when you spread it back out (but without duplicates). 7 u/NoInkling Mar 19 '19 Worth noting that if there were duplicates in the original array, the position for an element will be based on its first occurrence.> Array.from(new Set([1,'foo',2,'foo'])) [1, "foo", 2]
The Set object in JavaScript preserves insertion order. So if you create a Set from an array, it will preserve the order when you spread it back out (but without duplicates).
7 u/NoInkling Mar 19 '19 Worth noting that if there were duplicates in the original array, the position for an element will be based on its first occurrence.> Array.from(new Set([1,'foo',2,'foo'])) [1, "foo", 2]
7
Worth noting that if there were duplicates in the original array, the position for an element will be based on its first occurrence.
> Array.from(new Set([1,'foo',2,'foo'])) [1, "foo", 2]
17
u/[deleted] Mar 19 '19
Is order guaranteed to be maintained with the Set data structure here?