JS: 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
console.log(Array.from({ 0: "a", 1: "b", length: 2 }));
// [ "a", "b" ]
// Convert string to array of chars

console.log(
  JSON.stringify(Array.from("😃😄😅"))
);
// ["😃","😄","😅"]
Array.from(xlist, f)

Apply f to each.

The function f is passed 2 args:

  1. current element
  2. current index
console.log(
  Array.from("😃😄😅", (a, b) => [a, b]),
);
// [ [ "😃", 0 ], [ "😄", 1 ], [ "😅", 2 ] ]
Array.from(xlist, f, thisBinding)

JavaScript, Array-Like Object

JavaScript, List Object Properties (to Array)