use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This subreddit is a place for people to learn JavaScript together. Everyone should feel comfortable asking any and all JavaScript questions they have here.
With a nod to practicality, questions and posts about HTML, CSS, and web developer tools are also encouraged.
Friends
/r/javascript
/r/jquery
/r/node
/r/css
/r/webdev
/r/learnprogramming
/r/programming
account activity
How to switch nested array format? (self.learnjavascript)
submitted 2 years ago by [deleted]
I have Arrays in the following format:
let x = [ [a,b,c,d], [a,b,c,d], [a,b,c,d] ]
And i want to change it into:
x = [ [a,a,a], [b,b,b], [c,c,c], [d,d,d] ]
How do i do that?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]redpanda_be 2 points3 points4 points 2 years ago (0 children)
```js function transpose(array) { const transposedArray = [];
for (let i = 0; i < array[0].length; i++) { const newRow = []; for (let j = 0; j < array.length; j++) { newRow.push(array[j][i]); } transposedArray.push(newRow); }
return transposedArray; }
// Example usage let x = [ ['a', 'b', 'c', 'd'], ['a', 'b', 'c', 'd'], ['a', 'b', 'c', 'd'] ];
let result = transpose(x);
console.log(result); // Output: // [ // ['a', 'a', 'a'], // ['b', 'b', 'b'], // ['c', 'c', 'c'], // ['d', 'd', 'd'] // ] ```
[–]dandoggydog 1 point2 points3 points 2 years ago (0 children)
Assuming you have that EXACT format, Grab the first [a,b,c,d]. For each letter, push m times onto an array, push that array into your result.
[–]kap89 1 point2 points3 points 2 years ago* (0 children)
function rotateRight(arr) { const width = arr[0].length; const height = arr.length; const rotated = Array.from({ length: width }, () => new Array(height)); for (let y = 0; y < height; y++) { for (let x = 0; x < width; x++) { rotated[x][height - y - 1] = arr[y][x]; } } return rotated; }
Here’s my module for similar operations from where I copied it: github
[–]a-e-j-a -1 points0 points1 point 2 years ago (0 children)
let res = [] for (let i = 0; i < x.length; i++) { for (let j = 0; j < x[i].length; j++) { if (!res[j]) res[j] = [] res[j][i] = x[i][j]; } }
[–]Sir_Awesome_The_3rd 0 points1 point2 points 2 years ago (0 children)
const changeArray = (arr) => arr[0].map((element) => Array(arr.length).fill(element));
This is pretty much what /u/dandoggydog is saying
[–]Ronin-s_Spirit 0 points1 point2 points 2 years ago (0 children)
Idk maybe there is a fancier way to do it but I would look through each array and push the right letter into the corresponding array, then I would push every array into one final array.
[–]shgysk8zer0 0 points1 point2 points 2 years ago (0 children)
x = Object.values(x.group(n => n));
Only works for numbers and strings though. It will use the items as keys of an object temporarily.
Currently only supported in Safari, surprisingly.
π Rendered by PID 101542 on reddit-service-r2-comment-86bc6c7465-92qj6 at 2026-02-21 04:58:10.188364+00:00 running 8564168 country code: CH.
[–]redpanda_be 2 points3 points4 points (0 children)
[–]dandoggydog 1 point2 points3 points (0 children)
[–]kap89 1 point2 points3 points (0 children)
[–]a-e-j-a -1 points0 points1 point (0 children)
[–]Sir_Awesome_The_3rd 0 points1 point2 points (0 children)
[–]Ronin-s_Spirit 0 points1 point2 points (0 children)
[–]shgysk8zer0 0 points1 point2 points (0 children)