JavaScript from()Method
Definition and Usage
The from() method returns an array from an object that has 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 specify the first browser version that fully supports the 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 convert to an array. |
| mapFunction | Optional. The function to call on each element in the array. |
| thisValue | Optional. The this object in the mapping function (mapFunction). |
Technical Details
| Return value: | An Array object. |
| JavaScript Version: | ECMAScript 6 |
More Examples
The following example returns an array of objects contained in a Set.
var setObj = new Set(["a", "b", "c"]);
var objArr = Array.from(setObj);
objArr[1] == "b"; // true
The following example demonstrates how to use arrow syntax and a mapping function to change element values.
var arr = Array.from([1, 2, 3], x => x * 10);
// arr[0] == 10;
// arr[1] == 20;
// arr[2] == 30;
Other Extensions
JavaScript Array Object