Duplicate an array an arbitrary number of times (javascript)

前端 未结 9 1195
臣服心动
臣服心动 2021-01-17 17:35

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

9条回答
  •  遥遥无期
    2021-01-17 18:01

    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 }
        }
    }
    

提交回复
热议问题