THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.
This post talks about 3 ways to convert Set
type to Array
in JavaScript.
forEach
loop is one of the loop syntax to iterate the elements in collections of set type.
Its syntax forEach(callback,[arg]).
the callback is the function that is called for each element of a collection.
It takes three input parameters
const arrayObj = [];
mySet.forEach(element=> arrayObj.push(element));console.log(arrayObj); //[ 21, 3, 10 ]
console.log(typeof arrayObj); //object
ES6 introduced spread operator syntax..
Spread operator (..) allows to expand of the collection and expected values of at least one element. It is simple with the spread operator
var mySet = new Set([21, 3, 10,21]);
let arrayObj= [...mySet];
console.log(arrayObj);//[ 21, 3, 10 ]
console.log(typeof arrayObj); //object
from() function create and returns a new array from an input iterate collection such as an array, map, or set, and each element is iterated.
Syntax is Array.from(collection of objects);
var mySet = new Set([21, 3, 10,21]);
let arrayObj=Array.from(mySet.values());
console.log(arrayObj); //[ 21, 3, 10 ]
console.log(typeof arrayObj); //object
🧮 Tags
Recent posts
Puppeteer Login Test example How to convert Double to Integer or Integer to double in Dart| Flutter By Example Best ways to fix 504 gateway time out in nodejs Applications Fix for Error No configuration provided for scss Multiple ways to List containers in a Docker with examplesRelated posts