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

TypeScript – Array toString()

I need to convert this: ['CL-0191', 7, 'Surtidor 7', 2]

to this: ('CL-0191', 7, 'Surtidor 7', 2)

//I try using toString()

let arr = ['CL-0191', 7, 'Surtidor 7', 2]
let str = arr.toString()  // CL-0191,7,Surtidor 7,2

result: CL-0191,7,Surtidor 7,2
but I want as result ('CL-0191', 7, 'Surtidor 7', 2)

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

How to get it?

>Solution :

JSON.stringify almost does what you want, but you’ll have to manually replace the square brackets with parentheses and the double quotes with single quotes:

let str = JSON.stringify(arr).replaceAll('"', '\'').replace('[', '(').replace(']',')')
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