Create an array with same element repeated multiple times
>>> Array.apply(null, Array(10)).map(function(){return 5})[5, 5, 5, 5, 5, 5, 5, 5, 5, 5]>>> //Or in ES6>>> [...Array(10)].map((_, i) => 5)[5, 5, 5, 5, 5, 5, 5, 5, 5, 5]
you can try:
Array(6).join('a').split(''); // returns ['a','a','a','a','a'] (5 times)
Update (01/06/2018):
Now you can have a set of characters repeating.
new Array(5).fill('a'); // give the same result as above;// orArray.from({ length: 5 }).fill('a')
Note: Check more about fill(...) and from(...) for compatibility and browser support.
Update (05/11/2019):
Another way, without using fill
or from
, that works for string of any length:
Array.apply(null, Array(3)).map(_ => 'abc') // ['abc', 'abc', 'abc']
Same as above answer. Adding for sake of completeness.