我目前有这样一个数组:var arr = []如何使用for循环将多个 "hello "字符串推入数组?
var newArray = arr.push("hello")10;
尝试newArray forEach
或简单的for-loop应该可以。
var arr = [];
// method 1
new Array(5).fill(0).forEach(() => arr.push("hello"));
// alternate method
for (let i = 0; i < 5; i++) {
arr.push("world");
}
console.log(arr);
// Updating based on suggesion @mplungjan, quick way without loop.
var arr2 = Array(10).fill("hello");
console.log(arr2)