简体   繁体   English

我将如何使用 for 循环而不是使用 [...Array].map() 方法重写此代码?

[英]How would I rewrite this code using a for loop instead of using the […Array].map() method?

Here is the code, it uses map to go through and do the function.这是代码,它使用 map 到 go 并执行 function。 However if I want to make it do a loop using for (let i = 0) how would I go about that?但是,如果我想使用 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));

By this way通过这种方式

 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.

相关问题 我将如何遍历 Javascript 中的数组,添加一个单词,然后返回一个数组(不使用 map)? - How would I loop through an array in Javascript, add a word, then return an array (without using map)? 使用 JavaScript,我将如何使用 for 循环增加数组中的数字? - With JavaScript how would I increment numbers in an array using a for loop? 如何在Ruby中重写这个JavaScript循环? - How would I rewrite this JavaScript loop in Ruby? 尝试使用 map 方法而不是使用 React Typescript 的 for 循环 - Trying to use map method instead of for loop using React Typescript Javascript-使用映射函数而不是嵌套的for循环反规范化JSON数组 - Javascript - denormalize JSON array using map function instead of nested for loop 链接数组方法(过滤器、map、reduce)而不是使用双循环 - Chaining array methods (filter, map, reduce) instead of using double loop 使用 for 循环而不是 map 以小写形式返回数组中的所有值 - Return all values from array in lowercase using for loop instead of map 如何仅使用jQuery重写此JavaScript代码 - How to rewrite this javascript code using only jQuery instead 如何在反应中使用地图方法循环图像? - How to loop images using map method in react? 如何使用JavaScript数组映射方法编写循环代码 - how to write for loop code with JavaScript Array map Method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM