-4

在这里,我尝试了解如何创建数组数组:我创建了一个数组,但是如何创建一个数组数组,其中每个数组都有 10 个随机数?

var arrRand = [];
    while(arrRand.length < 10){
        var random = Math.floor(Math.random() * 10) + 1;
        if(arrRand.indexOf(random) === -1) arrRand.push(random);
    }
    console.log(arrRand);
4

4 回答 4

2

每个数字都是随机的功能方法。

let x = Array(4).fill().map(
  () => Array(10).fill().map(
    () => Math.floor(Math.random() * 10)
  )
);

console.log(x);

于 2020-04-24T09:40:59.960 回答
1

您可以使用Math.random嵌套的for loop. 这是一个例子:

let arr = [];
for(let i = 0; i < 4; i++){
     let current = [];
     for(let j = 0; j < 10; j++)
          current.push(Math.floor(Math.random() * 10));
     arr.push(current);
}
console.log(arr)

于 2020-04-24T09:28:48.847 回答
1

为了保持代码干净干爽,可以使用ES6 语法中的map函数。

 const x = [...Array(6)].map(
    () => [...Array(10)].map(
        () => Math.floor(Math.random() * 10) + 1
    )
 )

console.log(x)

于 2020-04-24T09:55:30.727 回答
-1
let a = Array(4).fill(Array(10).fill(null))

Math.random()然后用循环填充它

于 2020-04-24T09:36:50.867 回答