Array.from Function (Array) (JavaScript)

Returns an array from an array-like or iterable object.

Syntax

Array.from (arrayLike [ , mapfn [ , thisArg ] ] );  

Parameters

arrayLike
Required. An array-like object or an iterable object.

mapfn
Optional. A mapping function to call on each element in arrayLike.

thisArg
Optional. Specifies the this object in the mapping function.

Remarks

The arrayLike parameter must be either an object with indexed elements and a length property or an iterable object, such as a Set object.

The optional mapping function is called on each element in the array.

Example

The following example returns an array from a collection of DOM element nodes.

var elemArr = Array.from(document.querySelectorAll('*'));  
var elem = elemArr[0]; // elem contains a reference to the first DOM element  

Example

The following example returns an array of characters.

var charArr = Array.from("abc");  
// charArr[0] == "a";  

Example

The following example returns an array of objects contained in the collection.

var setObj = new Set("a", "b", "c");  
var objArr = Array.from(setObj);  
// objArr[1] == "b";   

Example

The following example shows the use of arrow syntax and a mapping function to change the value of elements.

var arr = Array.from([1, 2, 3], x => x * 10);  
// arr[0] == 10;  
// arr[1] == 20;  
// arr[2] == 30;  

Requirements

Supported in Microsoft Edge (Edge browser). Also supported in Store apps (Microsoft Edge on Windows 10). See Version Information.
Not supported in the following document modes: Quirks, Internet Explorer 6 standards, Internet Explorer 7 standards, Internet Explorer 8 standards, Internet Explorer 9 standards, Internet Explorer 10 standards, Internet Explorer 11 standards. Not supported in Windows 8.1.