I want to be able to get from [2, 3] and 3 : [2, 3, 2, 3, 2, 3]. (Like 3 * a in python where a is a list)
Is there a quick and efficient way to do this in Javascript ? I do this with a for loop, but it lacks visibility and I guess efficiency.
I would like for it to work with every types of element.
For instance, I used the code :
function dup (n, obj) { var ret = []; for (var i = 0; i<n; i++) { ret[i] = obj; } return (ret); } The problem is that it doesn't work with arrays or objects, only with primitive values.
Do I have to make conditions, or is there a clean way to duplicate a variable ?
array.prototype.map, if you want them multiplied all together usearray.prototype.reduce. However using a loop in JavaScript is actually the fastest way.