JavaScript: Array.from

By Xah Lee. Date: . Last updated: .

New in JS2015.

Array.from(xlist)
Convert xlist to array. xlist is a Array-Like Object or Iterable Object object.
// Convert Array-Like Object to Array
const xx = { 0: "a", 1: "b", length: 2 };
const yy = Array.from(xx);

console.log(Array.isArray(yy));

console.log(JSON.stringify(yy) === '["a","b"]');
// Convert set object (Iterable) to array.

const ss = new Set([3, 4, 5]);
const rr = Array.from(ss);
console.log(Array.isArray(rr));
rr;
// [ 3, 4, 5 ]
Array.from(xlist, f)
Apply f to each.

The function f is passed 2 args:

  1. current_element
  2. current_index
const xx = new Set(["a", "b", "c"]);

const yy = Array.from(xx, (x, y) => [x, y]);

console.log(yy);
// [ [ "a", 0 ], [ "b", 1 ], [ "c", 2 ] ]
Array.from(xlist, f, thisArg)
Use thisArg as this Binding in f. If thisArg is not given, undefined is used.

Before JS2015

See: Array-Like Object to Array

JavaScript: Array-Like Object

BUY
ΣJS
JavaScript in Depth

JS Obj Reference

Array

prototype