Just pass the array to the Set constructor. The Set constructor accepts an iterable parameter. The Array object implements the iterable protocol, so its a valid parameter.
var arr = [55, 44, 65];
var set = new Set(arr);
console.log(set.size === arr.length);
console.log(set.has(65));
Run code snippetEdit code snippet Hide Results Copy to answer Expand
See here
Answer from levi on Stack OverflowJust pass the array to the Set constructor. The Set constructor accepts an iterable parameter. The Array object implements the iterable protocol, so its a valid parameter.
var arr = [55, 44, 65];
var set = new Set(arr);
console.log(set.size === arr.length);
console.log(set.has(65));
Run code snippetEdit code snippet Hide Results Copy to answer Expand
See here
If you start out with:
let array = [
{name: "malcom", dogType: "four-legged"},
{name: "peabody", dogType: "three-legged"},
{name: "pablo", dogType: "two-legged"}
];
And you want a set of, say, names, you would do:
let namesSet = new Set(array.map(item => item.name));
if no such option exists, then maybe there is a nice idiomatic one-liner for doing that ? like, using
for...of, or similar ?
Indeed, there are several ways to convert a Set to an Array:
- Using
Array.from:
Note: safer for TypeScript.
const array = Array.from(mySet);
- Simply
spreadingthe Set out in an array:
Note: Spreading a Set has issues when compiled with TypeScript (See issue #8856). It's safer to use Array.from above instead.
const array = [...mySet];
- The old-fashioned way, iterating and pushing to a new array (Sets do have
forEach):
const array = [];
mySet.forEach(v => array.push(v));
via https://speakerdeck.com/anguscroll/es6-uncensored by Angus Croll
It turns out, we can use spread operator:
var myArr = [...mySet];
Or, alternatively, use Array.from:
var myArr = Array.from(mySet);
The use of either Array or Set by your function is equivalent: You create a container, and you push stuff into it. This leaves two areas for consideration:
What does the consumer need to do with the container?
Which container is more efficient (space or time)?
If you find that the consumer just iterates over the container, both Array and Set are equivalent again from this usage perspective. However, if your user needs to answer the question "is this index in the container?", then Set is the only appropriate container type.
Once you have asserted that the container type is indeed irrelevant to use, both on the producer and consumer side, go for the more efficient container:
Arrayshould be implemented as a simple slap of contiguous memory that holds (the references to) its elements. Apush()generally means 1) checking for space (one comparison), 2) appending the new element to the end of the buffer, and 3) updating the length field.Setis required to ensure that its elements are unique. That is, whereArray.push()is happy to push an element twice,Set.insert()must somehow catch this condition and handle it. This requires extra work. To getO(1)performance,Setwould normally be implemented as a hash table. Hash tables are fast, but they are definitely more complex than a simple, contiguous buffer. Thus,Set.insert()will generally have to do significantly more work thanArray.push(), and its data structures are very likely to consume more memory.
Both space and time efficiency are better for Array than for Set. And this is why I would default to using Array over Set whenever their use is equivalent.
From what you have shown in your question, it does not matter (and consider to refactor your code in case it starts to matter).
not limited to this case, if I found both Array and Set can finish my task, should I use Array or Set
Producing an Array or a Set is typically not an end in itself, it is a means to an end, so often you can find out what the caller of your method which produces this result might prefer.
Note, if you start using an array, and it turns out for further processing a Set would be beneficial (or you need both, the array and the set representation), you can easily convert it:
let resultSet = new Set(results)
I am not a Javascript expert, but I would expect the conversion the other way round to require at a little bit more code. Thus, if in doubt, I personally would start with an array first, and switch to a set if it fits better, but that is somewhat opinionated.