我有一个包含10个元素的数组,每个元素包含" "
。
如何创建一个空格字符串,如下所示:
" "
来自这个数组的javascript或jQuery?
谢谢
答案 0 :(得分:23)
很简单,请在地址栏中自行尝试:
javascript:alert('“'+new Array(42).join(' ')+'”')
顺便说一下,“在jquery中”应该是“使用jquery”
答案 1 :(得分:8)
您可以使用Array.join()
,如下所示:
var myArray = [" "," "," "," "," "," "," "," "," "," "];
var myString = myArray.join(''); //mySting is a string of 10 spaces
您需要将''
传递给.join()
,因为默认的连接符是逗号。
答案 2 :(得分:2)
你可以使用join。 例如:
var x = ['a', 'b', 'c', 'd'];
var y = x.join('');