2

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 ?

3
  • 5
    I don't understand how a loop "lacks visibility and efficiency". Commented May 3, 2015 at 11:40
  • Better description please. I don't really get your issue if for loop is inefficient. And I am not such a python pro. Commented May 3, 2015 at 11:44
  • If you want to multiple all entries by the same number you can use array.prototype.map, if you want them multiplied all together use array.prototype.reduce. However using a loop in JavaScript is actually the fastest way. Commented May 3, 2015 at 11:47

2 Answers 2

2

You can use this (very readable :P) function:

function repeat(arr, n){ var a = []; for (var i=0;i<n;[i++].push.apply(a,arr)); return a; } 

repeat([2,3], 3) returns an array [2, 3, 2, 3, 2, 3].

Basically, it's this:

function repeat(array, times){ var newArray = []; for (var i=0; i < times; i++){ Array.prototype.push.apply(newArray, array); } return newArray; } 

we push array's values onto newArray times times. To be able to push an array as its values (so, push(2, 3) instead of push([2, 3])) I used apply, which takes an array an passes it to push as a list of arguments.

Or, extend the prototype:

Array.prototype.repeat = function(n){ var a = []; for (var i=0;i<n;[i++].push.apply(a,this)); return a; } 

[2, 3].repeat(3) returns an array [2, 3, 2, 3, 2, 3].

If you want something reasonably readable, you can use concat within a loop:

function repeat(array, n){ var newArray = []; for (var i = 0; i < n; i++){ newArray = newArray.concat(array); } return newArray; } 
Sign up to request clarification or add additional context in comments.

5 Comments

his way lacks visibility. your way is hidden. xD
it's not about name.[i++].push.apply(a,this) is very complex.you can do it like a = a.concat(arr) inside the loop.maybe it will be visible xD
push.apply allows you to concatenate without the creation of a new variable tho.
@OmarElawady That's cool! I didn't know about .concat! Thanks, I'll use it to clarify my answer :)
@Andy the idea is that [i++].push.apply(a,this) is hard to understand.if push is better he can do it Array.prototype.push.apply(a, this).they're the same but I think the second is clearer.
-2

There is not. This is a very Pythonic idea.

You could devise a function to do it, but I doubt there is any computational benefit because you would just be using a loop or some weird misuse of string functions.

5 Comments

The question is about JavaScript, not PHP.
Thanks, I've removed the PHP reference. I got confused :P
it not works if array like [23,23]
You should be returning integers, not strings.
Like I said it was weird, and I was not recommending that piece of code nor do I wish to perfect it. It was just an example of how join() and split() can be used to generate an array without using a loop. I have removed it as it seems it was drawing negative attention.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.