用逗号和空格加入数组 [英] Join an array by a comma and a space

查看:104
本文介绍了用逗号和空格加入数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组,我想转换为逗号分隔的字符串。 Array.toString()有效,但是如果我有一个相当大的数组,它将不会换行,因为逗号后面没有空格:

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?

示例输出:

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


推荐答案

在JavaScript中有 .join() 方法开启数组获取字符串,您可以提供分隔符。在你的情况下,它看起来像这样:

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

你可以在这里测试一下

这篇关于用逗号和空格加入数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆