For reasons that I donβt understand, you cannot directly add the contents of one set to another using the built-in operation. Operations such as union, intersection, merging, etc., are fairly simple operations on sets, but are not built-in. Fortunately, you can pretty easily build it all yourself.
Thus, to implement the merge operation (merging the contents of one set into another or one card into another), you can do this with one line .forEach() :
var s = new Set([1,2,3]); var t = new Set([4,5,6]); t.forEach(s.add, s); console.log(s);
And for Map you can do this:
var s = new Map([["key1", 1], ["key2", 2]]); var t = new Map([["key3", 3], ["key4", 4]]); t.forEach(function(value, key) { s.set(key, value); });
Or in ES6 syntax:
t.forEach((value, key) => s.set(key, value));
For your information, if you need a simple subclass of the built-in Set object that contains the .merge() method, you can use this:
// subclass of Set that adds new methods // Except where otherwise noted, arguments to methods // can be a Set, anything derived from it or an Array // Any method that returns a new Set returns whatever class the this object is // allowing SetEx to be subclassed and these methods will return that subclass // For this to work properly, subclasses must not change behavior of SetEx methods // // Note that if the contructor for SetEx is passed one or more iterables, // it will iterate them and add the individual elements of those iterables to the Set // If you want a Set itself added to the Set, then use the .add() method // which remains unchanged from the original Set object. This way you have // a choice about how you want to add things and can do it either way. class SetEx extends Set { // create a new SetEx populated with the contents of one or more iterables constructor(...iterables) { super(); this.merge(...iterables); } // merge the items from one or more iterables into this set merge(...iterables) { for (let iterable of iterables) { for (let item of iterable) { this.add(item); } } return this; } // return new SetEx object that is union of all sets passed in with the current set union(...sets) { let newSet = new this.constructor(...sets); newSet.merge(this); return newSet; } // return a new SetEx that contains the items that are in both sets intersect(target) { let newSet = new this.constructor(); for (let item of this) { if (target.has(item)) { newSet.add(item); } } return newSet; } // return a new SetEx that contains the items that are in this set, but not in target // target must be a Set (or something that supports .has(item) such as a Map) diff(target) { let newSet = new this.constructor(); for (let item of this) { if (!target.has(item)) { newSet.add(item); } } return newSet; } // target can be either a Set or an Array // return boolean which indicates if target set contains exactly same elements as this // target elements are iterated and checked for this.has(item) sameItems(target) { let tsize; if ("size" in target) { tsize = target.size; } else if ("length" in target) { tsize = target.length; } else { throw new TypeError("target must be an iterable like a Set with .size or .length"); } if (tsize !== this.size) { return false; } for (let item of target) { if (!this.has(item)) { return false; } } return true; } } module.exports = SetEx;
This should be your own setex.js file, which can then be require() placed in node.js and used instead of the built-in set.