Creating Comma-Separated Lists from JavaScript Arrays
Converting a JavaScript array of strings into a comma-separated list is a common operation. Here's how you can achieve this with ease:
Using Array.prototype.join()
The Array.prototype.join() method is the most straightforward way to create a comma-separated list. It takes a separator as its argument, which by default is a comma and space.
<code class="js">var arr = ["Zero", "One", "Two"]; document.write(arr.join(", "));</code>
Result: "Zero, One, Two"
You can specify a different separator if needed, for example:
<code class="js">document.write(arr.join("; "));</code>
Result: "Zero; One; Two"
The above is the detailed content of How to Create a Comma-Separated List from a JavaScript Array?. For more information, please follow other related articles on the PHP Chinese website!