NOTE 2 The of function is an intentionally generic factory method; it
does not require that its this value be the Array constructor.
Therefore it can be transferred to or inherited by other constructors
that may be called with a single numeric argument.
So we may bind Array.of() to a function and generate an array like object.
function dummy(){};
var thingy = Array.of.apply(dummy,[1,2,3,4]);
console.log(thingy);
You could use a function like this:
This one should handle sparse arrays more efficiently.
As of Lodash 3.0.0 you can use _.toPlainObject
For completeness, ECMAScript 2015(ES6) spreading. Will require either a transpiler(Babel) or an environment running at least ES6.
Surprised not to see -
A quick and dirty one:
I would do this simply with
Array.of()
. Array of has the ability to use it's context as a constructor.So we may bind
Array.of()
to a function and generate an array like object.By utilizing
Array.of()
one can even do array sub-classing.