-4

Here I try to understand how to create array of arrays: I created one array but how to create an array of arrays, in which every array has 10 random numbers?

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);
6
  • 2
    Already have plenty of answers here on stackoverflow for such questions, break it down into 2 questions and google it Commented Apr 24, 2020 at 9:25
  • best way to generate empty 2D array Commented Apr 24, 2020 at 9:26
  • Creating array of length n with random numbers Commented Apr 24, 2020 at 9:28
  • 1
    @Leonardo Your solution would be great if it wouldn't use the same number to fill the arrays. Commented Apr 24, 2020 at 9:36
  • 1
    @NiklasE. good point 👍; btw see how people copy paste this inaccurate comment into answers Commented Apr 24, 2020 at 9:39

4 Answers 4

2

A functional approach with every number being random.

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

console.log(x);

Sign up to request clarification or add additional context in comments.

3 Comments

yeah this works; the outter .map() can be safely omitted by the way; I'd tested it in browser console
@Leonardo Elaborate. The way I see it, the outer map function is necessary and cannot be omitted. Otherwise it would be the same array four times.
yeah both maps are necessary; +1
1

You can use Math.random and a nested for loop. Here is an example:

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)

Comments

1

In order to keep clean and dry code, you can use map function in ES6 syntax.

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

console.log(x)

4 Comments

Interesting how you're using [...Array(10)] instead of Array(10).fill() to make them mappable, but that's not better actually: It only saves 2 characters and it is less efficient, because the interpreter would initialize two arrays, iterating through the inner to generate the outer. I would stick to my answer.
[...Array(6)] is useless redundant version of Array(6)
@NiklasE. yeah; correct; it's not. btw I'm seeing people mindlessly over-using the spared syntax everywhere; not here btw; it's legit. but I prefer Array.fill()
By using spread syntax, the code is more consistent and clear because we can notice easily [...Array(6)] as it is an array in the map function. Not a big deal with the code performance but it brings a clean code and intuitive sense.
-1
let a = Array(4).fill(Array(10).fill(null))

Then fill it with Math.random() in loop

1 Comment

Only half an answer. And unnecessary if the missing part can be done together with the first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.