JS: Array.from
(new in ECMAScript 2015)
Array.from(xlist)-
Convert xlist to array.
arg can be Array-Like Object or Iterable Object object.
If arg is Sparse Array, empty slots are treated as having value of undefined
// If arg is Sparse Array, empty slots are treated as having value of undefined console.log(Array.from(Array(3))); // [ undefined, undefined, undefined ] // 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(Array.from("🦋⭐🌞")); // [ "🦋", "⭐", "🌞" ] Array.from(xlist, f)-
Apply f to each.
The function f is passed 2 args:
- current element
- current index
// Convert string to array of chars 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.