Rafael Segura
Rafael Segura

Reputation: 59

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)

How to get it?

Upvotes: 2

Views: 1009

Answers (1)

Mureinik
Mureinik

Reputation: 311103

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(']',')')

Upvotes: 2

Related Questions