16

Let's say I'm given an array. The length of this array is 3, and has 3 elements:

var array = ['1','2','3'];

Eventually I will need to check if this array is equal to an array with the same elements, but just twice now. My new array is:

var newArray = ['1','2','3','1','2','3'];

I know I can use array.splice() to duplicate an array, but how can I duplicate it an unknown amount of times? Basically what I want is something that would have the effect of

var dupeArray = array*2;
slifty
  • 13,062
  • 13
  • 71
  • 109
kjsmita6
  • 458
  • 1
  • 6
  • 21
  • Do you want a function to compare two arrays or generate a new array from a given array? – Trott May 14 '15 at 03:50
  • `var arr=[1,2,3]; arr.map([].valueOf.bind(arr))` – dandavis May 14 '15 at 03:50
  • @Trott I have a function to compare the elements of two arrays. What I guess i'm looking for is a function to generate a new array an unknown amount of times from the given array. – kjsmita6 May 14 '15 at 03:51
  • 2
    At the point at which you are duplicating, you will know how many times you want to "duplicate" it, correct? Why can't you just use concat / splice / whatever multiple times? – slifty May 14 '15 at 03:51
  • 1
    *Eventually I will need to check if this array is equal to an array with the same elements, but just twice now.* Would this happen to be the X of your XY problem? In other words, is this your end goal? – Shashank May 14 '15 at 03:52
  • Have a look at [Array Replication Function in JavaScript using self-executing inner function](http://stackoverflow.com/q/22696267/1048572) – Bergi May 14 '15 at 03:59

9 Answers9

14
const duplicateArr = (arr, times) =>
    Array(times)
        .fill([...arr])
        .reduce((a, b) => a.concat(b));

This should work. It creates a new array with a size of how many times you want to duplicate it. It fills it with copies of the array. Then it uses reduce to join all the arrays into a single array.

Milesman34
  • 177
  • 1
  • 6
5

The simplest solution is often the best one:

function replicate(arr, times) {
     var al = arr.length,
         rl = al*times,
         res = new Array(rl);
     for (var i=0; i<rl; i++)
         res[i] = arr[i % al];
     return res;
}

(or use nested loops such as @UsamaNorman).

However, if you want to be clever, you also can repeatedly concat the array to itself:

function replicate(arr, times) {
    for (var parts = []; times > 0; times >>= 1) {
        if (times & 1)
            parts.push(arr);
        arr = arr.concat(arr);
    }
    return Array.prototype.concat.apply([], parts);
}
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • 1
    It should probably be `res = new Array(rl);` though no? And `i < rl` in the for loop. Regardless I upvoted. – Shashank May 14 '15 at 04:23
3

Basic but worked for me.

var num = 2;

while(num>0){
array = array.concat(array);
num--}
jmac
  • 89
  • 3
1

You can do:

var array = ['1','2','3'];


function nplicate(times, array){
      //Times = 2, then concat 1 time to duplicate. Times = 3, then concat 2 times for duplicate. Etc.
     times = times -1;
     var result = array;

    while(times > 0){
        result = result.concat(array);
        times--;
    }

    return result;
}

console.log(nplicate(2,array));

You concat the same array n times.

Use concat function and some logic: http://www.w3schools.com/jsref/jsref_concat_array.asp

1

Here's a fairly concise, non-recursive way of replicating an array an arbitrary number of times:

function replicateArray(array, n) {
  // Create an array of size "n" with undefined values
  var arrays = Array.apply(null, new Array(n)); 

  // Replace each "undefined" with our array, resulting in an array of n copies of our array
  arrays = arrays.map(function() { return array });

  // Flatten our array of arrays
  return [].concat.apply([], arrays);
}

console.log(replicateArray([1,2,3],4)); // output: [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]

What's going on?

The first two lines use apply and map to create an array of "n" copies of your array.

The last line uses apply to flatten our recently generated array of arrays.

Seriously though, what's going on?

If you haven't used apply or map, the code might be confusing.

The first piece of magic sauce here is the use of apply() which makes it possible to either pass an array to a function as though it were a parameter list.

Apply uses three pieces of information: x.apply(y,z)

  • x is the function being called
  • y is the object that the function is being called on (if null, it uses global)
  • z is the parameter list

Put in terms of code, it translates to: y.x(z[0], z[1], z[2],...)

For example

var arrays = Array.apply(null, new Array(n));

is the same as writing

var arrays = Array(undefined,undefined,undefined,... /*Repeat N Times*/);

The second piece of magic is the use of map() which calls a function for each element of an array and creates a list of return values.

This uses two pieces of information: x.map(y)

  • x is an array
  • y is a function to be invoked on each element of the array

For example

var returnArray = [1,2,3].map(function(x) {return x + 1;});

would create the array [2,3,4]

In our case we passed in a function which always returns a static value (the array we want to duplicate) which means the result of this map is a list of n copies of our array.

Community
  • 1
  • 1
slifty
  • 13,062
  • 13
  • 71
  • 109
  • Tricky code, but it sure isn't easy to follow what it does. I, for one, prefer code that anybody can immediately see what it does. There are no points for brevity in my book and there are points for clarity, ease of understanding. – jfriend00 May 14 '15 at 07:03
  • Fair enough! I was just combining answers from SO to show how you could look up subquestions to build a compound answer. I'll break it out a little more. – slifty May 14 '15 at 13:34
  • Updated plus a detailed explanation. Alas, it may be that `apply` and `map` are just always going to be advanced concepts that is a little hard to expect everyone to be able to get at first glance. – slifty May 14 '15 at 14:18
1

Keep it short and sweet

function repeat(a, n, r) {
    return !n ? r : repeat(a, --n, (r||[]).concat(a));
}

console.log(repeat([1,2,3], 4)); // [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]

http://jsfiddle.net/fLo3uubk/

Miguel Mota
  • 20,135
  • 5
  • 45
  • 64
1

if you are inside a loop you can verify the current loop index with the array length and then multiply it's content.

let arr = [1, 2, 3];

if(currentIndex > arr.length){ 
  //if your using a loop, make sure to keep arr at a level that it won't reset each loop

  arr.push(...arr);

}

Full Example: https://jsfiddle.net/5k28yq0L/

0

I think you will have to write your own function, try this:

function dupArray(var n,var arr){
var newArr=[];
for(var j=0;j<n;j++)
    for(var i=0;i<arr.length;i++){
        newArr.push(arr[i]);
    }
return newArr;
}
Usama Noman
  • 503
  • 1
  • 4
  • 10
  • The use will be like this: myArr = dupArray(5,previousArr); – Usama Noman May 14 '15 at 03:54
  • There are better ways to do this than nested for loops! Also you may want to edit your answer to include the usage, rather than writing it as a comment. – slifty May 14 '15 at 03:55
  • @slifty: Better ways such as? – Bergi May 14 '15 at 04:02
  • 1
    Good call ;) -- such as Array.concat https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat -- for instance http://stackoverflow.com/questions/10865025/merge-flatten-an-array-of-arrays-in-javascript – slifty May 14 '15 at 04:05
0

A rather crude solution for checking that it duplicates... You could check for a variation of the length using modulus:

Then if it might be, loop over the contents and compare each value until done. If at any point it doesn't match before ending, then it either didn't repeat or stopped repeating before the end.

if (array2.length % array1.length == 0){
    // It might be a dupe
    for (var i in array2){
        if (i != array1[array2.length % indexOf(i)]) { // Not Repeating }
    }
}
Doc Rinehart
  • 83
  • 11
  • When you use the "for var i in array2" syntax you're looping through the indices of array2. so this answer is really messed up. Perhaps using Array.prototype.forEach would be more elegant here. Using modulo is a good idea to solve this problem so I like that part of your answer. – Shashank May 14 '15 at 04:09