![](/img/trans.png)
[英]How would I loop through an array in Javascript, add a word, then return an array (without using map)?
[英]How would I rewrite this code using a for loop instead of using the […Array].map() method?
這是代碼,它使用 map 到 go 並執行 function。 但是,如果我想使用 for (let i = 0) 讓它做一個循環,我會怎么做 go 呢?
function randomArrayGenerator(n, low, high) {
var randoms = [...Array(n)].map(() => Math.random() * (high - low) + low);
return randoms;
}
console.log(randomArrayGenerator(10, 23, 51));
通過這種方式
function randomArrayGenerator(n, low, high) { let randoms = []; for(let i = 0; i < n; i++) { randoms.push(Math.random() * (high - low) + low); } return randoms; } console.log(randomArrayGenerator(10, 23, 51));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.