Warning: session_start(): open(/tmp/sess_a6f1a76b7ce001680150ba7158469a3d, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Convert A Set To An Array In JavaScript - LearnShareIT

How To Convert A Set To An Array In JavaScript

convert a Set to an Array in JavaScript

We will learn how to convert a set to an array in JavaScript using three different approaches. Each approach is relatively straightforward, and you can choose whichever you want. Read on this article now.

Convert a set to an array in JavaScript

Using a for loop 

The push() method adds one or more elements to the end of an array and returns the new length of the array:

Syntax:

push(value)

Parameter:

  • value: Value to push into your array

First, we initialize an array that has no element. We then loop through the set elements and push those values to the array using push(). As a result, the array will be fulfilled with the set elements:

let s = new Set([1,2,3]);
let a = [];

for (i of s)
    a.push(i);

console.log(a);

Output: 

[1,2,3]

This method is not only easy to understand but also the fundamental method of many built-in functions that can help you deal with this type of problem. In this tutorial, we only show you two popular built-in methods which doesn’t require external libraries that can help you cope with the problem.

Using Array.from()

Syntax:

Array.from(arrayLike)

Parameter:

  • arrayLike: An iterable such as a list or array-like object such as a set to be converted to an array.

The Array.from() method is intended to create shallow copies of Array instances from objects that are iterable, such as a Set. ES6 added this method to the Array object. This is what the code would look like:

let s = new Set([4,5,6]);
let a = Array.from(s);
console.log(a);

Output: 

[4,5,6]

If you are looking for an approach that doesn’t use any function, maybe you should consider using a three-dot operator (spread operator), this way is far more easy to use as introduced below.

Using spread syntax

You can use the Spread operator to create an array from a set. This was added to the JavaScript language as part of the ES6 standard. Its syntax converts sets to an array where a literal array is expected.

let set = new Set([9,7,0]);
let array = [...set];
console.log(array);

Output: 

[9,7,0]

As you can see, spread syntax makes our problem become much more succinct by using just three dots (…) and a name of the set you want to convert to. This method is also the most-used method of most developers to convert a set to an array in JavaScript.

Summary

We have learned how to convert a set to an array in JavaScript using three different solutions. It would help if you considered that each approach has its pros and cons. We hope you will be successful using our tutorial. Thank you for reading!

Maybe you are interested:

Leave a Reply

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