JavaScript: Array.from

By Xah Lee. Date: . Last updated: .

New in JS2015.

Array.from( items )
Convert items to array. items is a Array-Like Object or Iterable object.
Array.from( items, f )
Apply f to each.
Array.from( items, f, thisArg )
Use thisArg as this Binding in f. If thisArg is not given, undefined is used.

Example: Convert Array-Like Object to Array

// Convert Array-Like Object to Array
const arlike = {0:"a", 1:"b" , length:2};
console.log( Array.isArray ( arlike ) === false);
console.log( Array.isArray ( Array.from ( arlike ) ) );
 // all true

Example: Iterable to Array

Convert set object to array.

const ss = new Set ([3,4,5]); // a set object

// convert set to array object
const ar = Array.from(ss);

console.log(ar); // [ 3, 4, 5 ]

Example: With Map Function

Array.from( items, f ) example:

// a set object
const ss = new Set ([3,4,5]);

// convert to array, but add one to each
const ar = Array.from(ss, x => x +1);

console.log(ar); // [ 4, 5, 6 ]

Before JS2015

See: Array-Like Object to Array

JS in Depth
XAH  BUY NOW

JS in Depth

JS Obj Ref

DOM


JS Obj Ref

Array

prototype