Created
January 3, 2023 12:16
-
-
Save adarsh-chakraborty/1f34eab54dd57196227848c77bf40daa to your computer and use it in GitHub Desktop.
Transpose an array of strings in 2D
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let strs = ["abc" , "def", "ghi"]; | |
console.log(strs); | |
let strs2 = strs.map(item => { | |
return item.split(''); | |
}); | |
let transposed = transpose(strs2); | |
const final = transposed.map(item => { | |
return item.join(''); | |
}); | |
console.log(final); | |
function transpose(arr) { | |
return arr[0].map((_, colIndex) => { | |
console.log(colIndex); | |
return arr.map(row => { | |
console.log(row); | |
return row[colIndex] | |
}) }); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment