Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I construct a Set with an Array

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?

like image 420
Randomblue Avatar asked Jul 07 '13 19:07

Randomblue


People also ask

What does Set in array mean?

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.—

How do you add an array to a Set in C++?

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++.

Can we convert Set to array in Java?

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.

Which is the proper way to declare an 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.


2 Answers

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.

like image 108
phihag Avatar answered Oct 21 '22 08:10

phihag


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
like image 35
NO WAR WITH RUSSIA Avatar answered Oct 21 '22 07:10

NO WAR WITH RUSSIA