Array.from

from — Creates a new array from an array like input.

Description

Array.from(mixed array, [function callback, [object this]])

Parameters

Name Description Type Default Optional
array See array below. mixed No
callback See callback below function Yes
this object Yes
array

An array, or array like input. Something that has a length and indexed items.

callback

The following parameters is passed to the callback.

value

The value of the current index.

index

The current index.

Return values

array

Changelog

Version Description
ES 6 Introduced.

Examples

Example #1 – from example
console.log(Array.from([])); // [] console.log(Array.from([1, 2, 3])); // [1, 2, 3] console.log(Array.from(123)); // [] console.log(Array.from('123')); // ["1", "2", "3"] console.log(Array.from('String')); // ["S", "t", "r", "i", "n", "g"]
var result = Array.from([1, 2, 3], function (value, index) { console.log(value, index); return value * value; }); console.log(result); // [1, 4, 9]
var result = Array.from(new Set([1, 2, 3]), function (value, index) { console.log(value, index); return value * value; }); console.log(result); // [1, 4, 9]

External references