I have an array that 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
I have an array that 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
Share Improve this question edited Jul 23, 2010 at 11:12 Nick Craver 630k138 gold badges1.3k silver badges1.2k bronze badges asked Jul 23, 2010 at 11:06 user338195user338195 6 | Show 1 more comment5 Answers
Reset to default 24Easy, 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.
Try it with a padEnd()
string method:
let res = "".padEnd(10, " ");
console.log('start' + res + 'end');
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')
_
to make it clearer. – RoToRa Commented Jul 23, 2010 at 11:11<pre>
block will do the job, I updated the question :) – Nick Craver Commented Jul 23, 2010 at 11:12
! ;-) – Andy E Commented Jul 23, 2010 at 11:15