Last active
August 17, 2022 09:10
-
-
Save kennarddh/b7a479b19096af82aa5ca0ccfb792e9e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const merge2DArray = arr => { | |
return arr.reduce((acc, secondData) => { | |
if (acc.length < secondData.length) { | |
const loopTimes = secondData.length - acc.length | |
for (let i = 1; i <= loopTimes; i++) { | |
acc.push('') | |
} | |
} | |
secondData.map((value, index) => { | |
acc[index] += value | |
}) | |
return acc | |
}, []) | |
} | |
const data = [ | |
['A', 'B', 'C'], | |
['D', 'E', 'F', 'G'], | |
] | |
const expected = ['AD', 'BE', 'CF', 'G'] | |
console.log(merge2DArray(data)) | |
console.log(expected) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment