如何用另一个数组填充一个数组?

时间:2016-02-22 14:32:16

标签: javascript arrays

基本上我有这个带图像的数组:

var random_images = ["1.png","2.png","3.png","4.png","5.png","6.png","7.png","8.png","9.png","10.png","11.png","12.png","13.png","14.png","15.png","16.png","17.png","18.png","19.png","20.png","21.png" ];

我想将它们全部[随机]填充到另一个数组中:

 var myArray = new Array(100)  (as you can see i want to fill those images to 100 spaces and each time i refresh the page i need to scatter them randomly)

我试过这样做:

var combinedArray = myArray.concat(random_images);

它似乎不起作用。还有其他办法吗?

1 个答案:

答案 0 :(得分:1)

var newArray = new Array(100);

for (i = 0; i < 100; i++) {
    newArray[i] = random_images[Math.floor(Math.random() * random_images.length)];

您可以尝试这样的代码。