How to Convert Set into an Array in TypeScript

In TypeScript, Convert the Set into an Array by using the “Array.from” function or by using the spread operator “(`...`)”. In this article, we will learn How to convert “Set” into an Array in TypeScript.

convert set into an Array in Typescript

Using Array.from Method

Array.from” the method is used to create a new array from an iterable object.

Here is how you can use Array.from in TypeScript

const setArray = new Set([1001,1020,1400,2300,'Pointing','Stuart']);
const arry = Array.from(setArray);

console.log(arry);

Output:

[ 1001, 1020, 1400, 2300, ‘Pointing’, ‘Stuart’ ]

Using Spread Operator

const setArray = new Set([1001,1020,1400,2300,'Pointing','Stuart']);
const arrspreading = [...setArray];
console.log(arrspreading);

Related article – How to Create a Set in TypeScript

Leave a Reply

Your email address will not be published. Required fields are marked *