Reputation: 5379
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
Upvotes: 1
Views: 354
Reputation: 386570
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; }
Upvotes: 1
Reputation: 4649
let rGrid = [],
i = 0;
shuffled.forEach(function(each){
let arr = new Array(10);
arr[0] = each;
rGrid.push(arr);
});
Upvotes: 0
Reputation: 1507
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)
Upvotes: 0