Home > Software engineering >  TypeScript - Array toString()
TypeScript - Array toString()

Time:12-22

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?

CodePudding user response:

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