1

I've got an array of random numbers 1-10 and would like to create 10 arrays, each of them starting with a number from the random array like so:

const shuffled = [3, 6, 8, 2, 7, 1, 10, 4, 9, 5];

let rGrid = [];

// populate arrays
for (let x of shuffled) {
  rGrid[x-1] = new Array(10);
  rGrid[x-1][0] = x;
}
console.log(rGrid);

So ultimately I would like:

array1: 3,,,,,,,,,,,,
array2: 6,,,,,,,,,,,,
array3: 8,,,,,,,,,,,,
etc.

The code I have places ordered numbers 1-10 as first elements of arrays:

array1: 1,,,,,,,,,,,
array2: 2,,,,,,,,,,,
array3: 3,,,,,,,,,,

Please advise. Here's a code pen: http://codepen.io/wasteland/pen/QdvwJj?editors=1010

3 Answers 3

1

You could apply a new array for each element of shuffled and set the first element to the actual number.

var shuffled = [3, 6, 8, 2, 7, 1, 10, 4, 9, 5],
    array = shuffled.map(a => {
        var r = new Array(10);
        r[0] = a;
        return r;
    });

console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

Comments

0

This might help

   rgrid = [] 
   for x in shuffled
       var temp = []
       temp.push(x)
       for i < shuffled.length-1
         temp.push("")
       rgrid.push(temp)

  console.log(rgrid)

Comments

0
let rGrid = [], 
    i = 0;
shuffled.forEach(function(each){
    let arr = new Array(10);

    arr[0] = each;

    rGrid.push(arr);
});

Comments

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.