JavaScript from() Method
Example
Create an array from a string:
var
myArr
=
Array
.
from
(
"
RUNOOB
"
)
;
Try it »
Definition and Usage
The from() method returns an array from an object with a length property or an iterable object.
Returns true if the object is an array, otherwise returns false.
Browser support
The numbers in the table indicate the first browser version that supports this method.
| Method | |||||
|---|---|---|---|---|---|
| from() | 45.0 | 12.0 | 32.0 | 9 | 25.0 |
Syntax
Array.from(object, mapFunction, thisValue)
Parameters
| Parameters | Description |
|---|---|
| object | Required. The object to be converted into an array. |
| mapFunction | Optional. The function to call for each element in the array. |
| thisValue | Optional. The this object used in the mapping function (mapFunction). |
Technical Details
| Return value: | An array object. |
|---|---|
| JavaScript version: | ECMAScript 6 |
More examples
The example below returns an array of objects contained in a collection.
var
setObj
=
new
Set
(
[
"
a
"
,
"
b
"
,
"
c
"
]
)
;
var
objArr
=
Array
.
from
(
setObj
)
;
objArr
[
1
]
==
"
b
"
;
//
true
The example below demonstrates how to use arrow syntax and a mapping function to change the values of elements.
var
arr
=
Array
.
from
(
[
1
,
2
,
3
]
,
x
=>
x
10
)
;
//
arr[0] == 10;
//
arr[1] == 20;
//
arr[2] == 30;
JavaScript Array object