JS: Apply Array Method to Array-Like Object
After JS2015
to apply Array methods to Array-Like Object, use
// using a array method Array.prototype.map directly on array-like object const xarrayLike = { 0: "a", 1: "b", 2: "c", length: 3 }; function ff(x) { return x + "z"; } const xresult = Reflect.apply(Array.prototype.map, xarrayLike, [ff]); // result is a array console.log(Array.isArray(xresult)); console.log(xresult); // [ "az", "bz", "cz" ]
Before JS2015
use
// using a array method Array.prototype.map directly on array-like object const xarrayLike = { 0: "a", 1: "b", 2: "c", length: 3 }; function ff(x) { return x + "z"; } const xresult = Array.prototype.map.call(xarrayLike, ff); // result is a array console.log(Array.isArray(xresult)); // true console.log(xresult); // [ "az", "bz", "cz" ]