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 ?

Jean Gabriel
  • 155
  • 13
  • 5
    I don't understand how a loop "lacks visibility and efficiency". – JJJ May 03 '15 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. – jPO May 03 '15 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. – Mouser May 03 '15 at 11:47

2 Answers2

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;
}
theonlygusti
  • 7,942
  • 8
  • 40
  • 80
  • 2
    his way lacks visibility. your way is hidden. xD – Omar Elawady May 03 '15 at 11:56
  • 1
    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 – Omar Elawady May 03 '15 at 12:03
  • `push.apply` allows you to concatenate without the creation of a new variable tho. – Andy May 03 '15 at 12:04
  • @OmarElawady That's cool! I didn't know about `.concat`! Thanks, I'll use it to clarify my answer :) – theonlygusti May 03 '15 at 12:09
  • @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. – Omar Elawady May 03 '15 at 12:13
-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.

braks
  • 1,267
  • 11
  • 21
  • 2
    The question is about JavaScript, not PHP. – JJJ May 03 '15 at 11:48
  • Thanks, I've removed the PHP reference. I got confused :P – braks May 03 '15 at 11:51
  • it not works if array like [23,23] – Grundy May 03 '15 at 12:01
  • 1
    You should be returning integers, not strings. – Andy May 03 '15 at 12:02
  • 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. – braks May 03 '15 at 12:33