While Set API is still very minimalistic, you can use Array.prototype.forEach and shorten your code a bit:
array.forEach(item => mySet.add(item))
// alternative, without anonymous arrow function
array.forEach(mySet.add, mySet)
Answer from amankkg on Stack OverflowWhile Set API is still very minimalistic, you can use Array.prototype.forEach and shorten your code a bit:
array.forEach(item => mySet.add(item))
// alternative, without anonymous arrow function
array.forEach(mySet.add, mySet)
Here's a functional way, returning a new set:
const set = new Set(['a', 'b', 'c'])
const arr = ['d', 'e', 'f']
const extendedSet = new Set([ ...set, ...arr ])
// Set { 'a', 'b', 'c', 'd', 'e', 'f' }
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
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));
No there is not.
A Set works on objects and primitives and is useful for preventing identical primitives and re-adding the same object instance.
Each array is their own object, so you can actually add two different arrays with the same values.
var set = new Set();
set.add([3, 4]);
set.add([3, 4]);
console.log(set.size);//2
Additionally, there's nothing to prevent an object from being changed once in a set.
var set = new Set();
var a1 = [3, 4];
var a2 = [3, 4];
set.add(a1);
set.add(a2);
a2.push(5);
for (let a of set) {
console.log(a);
}
//Outputs:
// [3, 4]
// [3, 4, 5]
A set does not have a mechanism for checking the values of objects in a set. Since the value of an object could change at any time, it wouldn't be much more efficient than simply looping over them yourself.
The functionality you are looking for has been kicked around in various ECMAScript proposals:
Composites
Record and Tuple (withdrawn at stage 2)
It does not appear to be coming anytime soon.
It's not using a Set, but may solve your problem.
If you want to make sure that an array can only exist in a collection once, and you want instant lookups you can use a hash by abusing its keys. They do not necessarily need a value. You can set them to undefined, to true, or something else if it makes you sleep better at night 😛.
Inspired by: javascript search array of arrays
It is halfway tempting to make a wrapper, but it is also simple enough to handle a small job.
const hash = {};
hash[[1, 2, 3]] = undefined;
hash[[3, 4]] = undefined;
hash[[3, 4]] = undefined;
console.log({hash});
console.log("hash has [3, 4]?", hash.hasOwnProperty([3, 4]));
For me, this will work fine, because I am checking if a coordinate exists in a collection. Unfortunately from this stackoverflow it seems that Set cannot be the answer to this problem.
Coincidentally it seems to implicitly solve your problem of not calling toString on the array which this seems to do under the hood. Personally I don't care for hash.hasOwnProperty.
A More Elegant Option
You could even write a wrapper to your code like:
class ArraySet extends Set {
add(arr) {
super.add(arr.toString());
}
has(arr) {
return super.has(arr.toString());
}
}
const arraySet = new ArraySet();
arraySet.add([1, 2]);
arraySet.add([3, 4]);
arraySet.add([3, 4]);
console.log("ArraySet has [3, 4]?", arraySet.has([3, 4]));