JavaScript Array from() Method (original) (raw)

Last Updated : 12 Nov, 2024

The Array.from() method is used to create a new array from any iterables like array, objects, and strings.

JavaScript `

const a = Array.from("GeeksforGeeks"); console.log(a);

`

Output

[ 'G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's' ]

Syntax

Array.from(object, mapFunction, thisValue);

**Parameters

**Return value

It returns a new array from the iterable passed as argument.

We have a complete list of Javascript Array methods, to check those please go through this Javascript Array Complete reference article.

Creating Array from Set

We can use Array.from() method to create or convert the given set to the array.

JavaScript `

const s = new Set([10, 20, 30]); const a = Array.from(s); console.log(a);

`

Creating Array from Object

It can create array from the keys, values and also from each entry of the object.

JavaScript `

const obj = { a: 10, b: 20, c : 30, d : 40, e : 50 };

const a1 = Array.from(Object.keys(obj)); const a2 = Array.from(Object.values(obj)); const a3 = Array.from(Object.entries(obj));

console.log(a1); console.log(a2); console.log(a3);

`

Output

[ 'a', 'b', 'c', 'd', 'e' ] [ 10, 20, 30, 40, 50 ] [ [ 'a', 10 ], [ 'b', 20 ], [ 'c', 30 ], [ 'd', 40 ], [ 'e', 50 ] ]

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.