Lynn
Lynn

Reputation: 121

Easy way to turn JavaScript array into comma AND Quotation Marks separated string list?

I wanted to know if there was a function that could turn an array into a string separated with Quotation Marks and commas in JS?

var array = [1,2,3]

var result = '["1", "2", "3"]';

Upvotes: 2

Views: 500

Answers (1)

Majed Badawi
Majed Badawi

Reputation: 28414

You can use .map to convert array elements to strings, and JSON.stringify to get the resulting string:

const array = [1,2,3];

const result = JSON.stringify(array.map(String));

console.log(result);

Upvotes: 6

Related Questions