Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Convert multidimensional array to a string in javascript?

My input is a multidimensional array and I want everything in the array to be in one string –

Input –

let input = [[1,2,3],[1,3,2],[3,2,1],[3,1,2],[2,1,3],[2,3,,1]]

Expected Ouput

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

"123,132,321,312,213,231"

I have tried

input.join() // which gives me

"1,2,3,1,3,2,3,2,1,3,1,2,2,1,3,2,3,1"

And also I have tried

input.join('') // which gives me

"1,2,31,3,23,2,13,1,22,1,32,3,1"

I have also tried using a for loop for this

let input = [[1,2,3],[1,3,2],[3,2,1],[3,1,2],[2,1,3],[2,3,1]]
let output = ''

for (let i = 0; i < input.length; i++){
 let result = input[i]
 output += result.toString()
}

Which returns

 "123132321312213231"

I can’t seem to crack this… Any tips??

Thanks

>Solution :

Can be achieved simply using .map() with .join('') inside:

const input = [[1,2,3],[1,3,2],[3,2,1],[3,1,2],[2,1,3],[2,3,,1]];

const result = input.map(arr => arr.join('')).join();

console.log(result);

When you pass an empty string '' into .join(), all elements are joined without any characters in between them.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading