I am playing with Set
in Node.JS v0.11.3 and the --harmony
flag. The API works fine, I can add
, remove
, clear
, etc. I have however not been able to initialise a set with an array. I have tried (as prompted by the MDN page)
var mySet = new Set([1, 1, 2]);
How can I convert an array to a set? Is MDN outdated? Has Node.JS simply not implemented the feature?
2a : to set or place in order : draw up, marshal the forces arrayed against us. b : to set or set forth in order (something, such as a jury) for the trial of a cause. 3 : to arrange or display in or as if in an array The … data are arrayed in descending order.—
Convert an array to Set using begin() and end() In this method, we use STL function to convert begin() and end() to convert pointers to iterators. Then we will pass the iterators to set constructor to convert an array to a set. In this example we converted an array to a set in C++.
The toArray() method of Java Set is used to form an array of the same elements as that of the Set. Basically, it copies all the element from a Set to a new array.
The syntax for declaring an array is: datatype[] arrayName; datatype : The type of Objects that will be stored in the array eg. int , char etc.
The v8 implementation of the Set
constructor does not yet support the iterator
and comparator
arguments mentioned in §15.16.1.1 of the current draft of the Harmony specification, and node uses v8 as its JavaScript interpreter.
As a band-aid, you can use the simplesets package.
Works fine in v8 now using an array supplied to a constructor. I'm using node v6.2.0 (v8 version 5.0.71.47).
> let mySet = new Set([1,2,3]);
undefined
> mySet;
Set { 1, 2, 3 }
> for ( let key of mySet ) { console.log(key) }
1
2
3
undefined
> mySet.size
3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With