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 Overflow Top answer 1 of 12
264
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)
2 of 12
137
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' }
01:46
JavaScript tips — Convert a Set to an Array - YouTube
02:01
Add multiple values to an array using Array.push
JavaScript How To Add To An Array (js) - YouTube
10:40
Adding to Arrays: Javascript and NodeJS for Complete Beginners ...
07:38
5 Ways to Add Items to Arrays in JavaScript - YouTube
01:13
JavaScript tips — Convert an array to a Set - YouTube
W3Schools
w3schools.com › js › js_sets.asp
JavaScript Sets
JS Examples JS HTML DOM JS HTML Input JS HTML Objects JS HTML Events JS Browser JS Editor JS Exercises JS Quiz JS Website JS Syllabus JS Study Plan JS Interview Prep JS Certificate ... A JavaScript Set is a collection of unique values. Each value can only occur once in a Set. The values can be of any type, primitive values or objects. ... // Create a Set const letters = new Set(); // Add Values to the Set letters.add("a"); letters.add("b"); letters.add("c"); Try it Yourself »
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Set › add
Set.prototype.add() - JavaScript - MDN Web Docs - Mozilla
const set = new Set(); set.add(42); set.add(42); set.add(13); for (const item of set) { console.log(item); // Expected output: 42 // Expected output: 13 }
Medium
medium.com › @conboys111 › what-happens-when-you-add-objects-or-arrays-to-a-javascript-set-b9f15dbbc5e0
What Happens When You Add Objects or Arrays to a JavaScript Set? | by myHotTake | Medium
November 11, 2024 - Even though both arrays hold the same values, the Set treats them as different creatures because they occupy different places in memory — just like how I’d treat two identical magical creatures as unique unless they are the same in essence. Now, let’s try adding the same array reference twice:
Mastering JS
masteringjs.io › tutorials › fundamentals › add-to-array
Add an Element to an Array in JavaScript - Mastering JS
June 18, 2021 - So the below loop is O(n^2). const ... element should be unique. const set = new Set(['a', 'b', 'c']); set.add('a'); set; // Set(3) { 'a', 'b', 'c' } set.add('d'); set; // Set(4) { 'a', 'b', 'c', 'd' }...
W3Schools
w3schools.com › Jsref › jsref_set_add.asp
JavaScript Set add() Method
If an attempt is made to add an element that already exists in the set, the add() method will have no effect, and the set will remain unchanged. ... The add() method directly modifies the existing Set object.
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Set
Set - JavaScript - MDN Web Docs
August 13, 2026 - Takes a set and returns a new set containing elements in this set but not in the given set. ... Returns a new iterator object that contains an array of [value, value] for each element in the Set object, in insertion order.
Mimo
mimo.org › tutorials › javascript › how-to-add-to-an-array-in-javascript
How to Add to an Array in JavaScript
Use unshift() to add items to the start of an array. ... It shifts existing elements to the right. Use splice() when you need to insert at a certain index. ... First argument: index to start. Second argument: how many items to remove. Remaining arguments: items to insert.
GeeksforGeeks
geeksforgeeks.org › how-to-convert-array-to-set-in-javascript
How to Convert Array to Set in JavaScript? | GeeksforGeeks
October 25, 2024 - A union set is the combination of two elements. In mathematical terms, the union of two sets is shown by A ⪠B. It means all the elements in set A and set B should occur in a single array. In JavaScript, a union set means adding one set element to the other set.
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › TypedArray › set
TypedArray.prototype.set() - JavaScript - MDN Web Docs
July 10, 2025 - // Create an ArrayBuffer with a size in bytes const buffer = new ArrayBuffer(8); const uint8 = new Uint8Array(buffer); // Copy the values into the array starting at index 3 uint8.set([1, 2, 3], 3); console.log(uint8); // Expected output: Uint8Array [0, 0, 0, 1, 2, 3, 0, 0]
JSchallenger
jschallenger.com › javascript-practice › javascript-sets › add-multiple-elements-to-javascript-set
Add multiple elements to Set | JSchallenger
JSchallenger. Write a function that takes a Set and an array as arguments. If not already existing, add each element in the array to the Set. Return the modified Set
Medium
medium.com › @conboys111 › how-to-easily-convert-a-set-to-an-array-and-vice-versa-in-javascript-a4e28ac9ccef
How to Easily Convert a Set to an Array and Vice Versa in JavaScript | by myHotTake | Medium
November 4, 2024 - This is the flexibility of arrays — they don’t care about uniqueness, just the order of things. On the other hand, if I want to ensure that no duplicate artifacts are ever allowed in my collection, I can always use a Set. Let’s say I find some new artifacts but don’t want to allow duplicates: // Adding new artifacts to the display case, ensuring no duplicates displayCase.add("Painting"); displayCase.add("Coin"); // "Coin" already exists, so it won't be added again console.log(displayCase); // Set { "Coin", "Vase", "Sculpture", "Painting" }
Scaler
scaler.com › home › topics › how to convert set to array in javascript
How to Convert Set to Array in JavaScript - Scaler Topics
May 4, 2023 - Where an array literal is expected, the spread syntax enables the expansion of the set. This was added to the JavaScript ES6 specification. ... Example - 1: Converting a set into a new array by using spread operator.
freeCodeCamp
freecodecamp.org › news › how-to-insert-an-element-into-an-array-in-javascript
Push into an Array in JavaScript – How to Insert an Element into an Array in JS
November 7, 2024 - If we want to add to the array, we set the second argument to zero (0), instructing the splice() method not to delete any array elements. The following parameter(s) or element(s) may be more than one, as these are the elements we want to add to the array at the specified position. For example, let's place "Kenya" after "Ghana" in our countries array: