Join an array by a comma and a space

JavascriptArraysStringTostring

Javascript Problem Overview


I have an array that I want converted to a comma delimited string. Array.toString() works, but if I have a rather large array it won't wrap because there are no spaces after the commas:

document.body.innerHTML = ['css','html','xhtml','html5','css3','javascript','jquery','lesscss','arrays','wordpress','facebook','fbml','table','.htaccess','php','c','.net','c#','java'].toString();
// css,html,xhtml,html5,css3,javascript,jquery,lesscss,arrays,wordpress,facebook,fbml,table,.htaccess,php,c,.net,c#,java

How can I have spaces after the commas in order to allow line/word wrapping?

Example output:

css, html, xhtml, html5, css3, javascript, jquery, lesscss, arrays, wordpress, facebook, fbml, table, .htaccess, php, c, .net, c#, java

Javascript Solutions


Solution 1 - Javascript

In JavaScript there's a .join() method on arrays to get a string, which you can provide the delimiter to. In your case it'd look like this:

var myArray = ['css','html','xhtml','html5','css3','javascript','jquery','lesscss','arrays','wordpress','facebook','fbml','table','.htaccess','php','c','.net','c#','java'];
var myString = myArray.join(', ');

You can test it out here

Solution 2 - Javascript

Use array.join(", "); and it should work

Solution 3 - Javascript

 string.Join(", ", new string[] { "css", "html", "xhtml", ..etc });

This prints the items with a comma and a space

[edit] I'm sorry, did not see it was for javascript. My code is c# :)

Solution 4 - Javascript

Try this way by the regex

let arr = ['css', 'html', 'xhtml', 'html5', 'css3', 'javascript', 'jquery', 'lesscss', 'arrays', 'wordpress', 'facebook', 'fbml', 'table', '.htaccess', 'php', 'c', '.net', 'c#', 'java'].toString();

let myString = arr.replace(/,[s]*/g, ", ");


console.log(myString);

Solution 5 - Javascript

I saw in a comment the question of how to make that without the .join() function.

Here is one tricky way:

const array_of_strings = ['css','html','xhtml','html5','css3','javascript','jquery','lesscss','arrays','wordpress','facebook','fbml','table','.htaccess','php','c','.net','c#','java']
const separator = ', '
const result = array_of_strings.reduce((accumulator, currentValue) => accumulator + separator + currentValue);
console.log(result)

Solution 6 - Javascript

Had to put an # in front of every word, the .join() didn't work for the first one and had to do this :

var myString = '#'+ myArray.join(', ');

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMyles GrayView Question on Stackoverflow
Solution 1 - JavascriptNick CraverView Answer on Stackoverflow
Solution 2 - JavascriptadarshrView Answer on Stackoverflow
Solution 3 - JavascriptJohn xyzView Answer on Stackoverflow
Solution 4 - JavascriptForce BoltView Answer on Stackoverflow
Solution 5 - JavascriptDmytro HuzView Answer on Stackoverflow
Solution 6 - JavascriptDessuaneView Answer on Stackoverflow