开发者

How to create string of blanks (" ") in javascript or jQuery?

I have an array t开发者_JAVA技巧hat contains 10 elements, each element contains " ".

How do I create a string of spaces, like this:

"          "

in javascript or jQuery from the this array?

Thank you


Easy, try it yourself in address box:

javascript:alert('“'+new Array(42).join(' ')+'”')

By the way, "in jquery" should be "using jquery"


You would use Array.join() for this, like this:

var myArray = [" "," "," "," "," "," "," "," "," "," "];
var myString = myArray.join(''); //mySting is a string of 10 spaces

You need to pass the '' to .join() because the default joiner is a comma.


You can use join for that. Example:

var x = ['a', 'b', 'c', 'd'];
var y = x.join('');


Try it with a fill() method:

let res = Array(10).fill(' ').join('')
console.log('start' + res + 'end')


Try it with a padEnd() string method:

let res = "".padEnd(10, " ");
console.log('start' + res + 'end');

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜