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

how to convert whole array to string in javascript

I got a question in the interview that he asked to convert the whole array into a String. I tried with toString() but that’s not worked for his output.

input

let a=['20','30','50']

expected output

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

"['20','30','50']"

>Solution :

Assuming the "output" is the content of this string you want, you can map the values first to wrap them in single-quotes, then join the array with commas and wrap the entire thing in "[...]".

const a = ['20','30','50'];

const str = `"[${a.map(v=>`'${v}'`).join(",")}]"`;

console.log(str);
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