JS: Array.from
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:
- current element
- current index
console.log( Array.from("😃😄😅", (a, b) => [a, b]), ); // [ [ "😃", 0 ], [ "😄", 1 ], [ "😅", 2 ] ]
Array.from(xlist, f, thisBinding)
-
- Use thisBinding as this (binding) in f.
- If thisBinding is not given, undefined is used.