Javascript 简化js代码:以数组形式获取两个数字之间的范围

Javascript 简化js代码:以数组形式获取两个数字之间的范围,javascript,arrays,simplify,Javascript,Arrays,Simplify,下面的代码可以简化吗 let arr = [] for (let value = startRange.value; value <= endRange.value; value++) { arr.push({value: value, someothervalue: value*2}) } return arr 输出将是 [ {value: 3, someothervalue: 6}, {value: 4, someothervalue: 8}, {value: 5,

下面的代码可以简化吗

let arr = []
for (let value = startRange.value; value <= endRange.value; value++) {
  arr.push({value: value, someothervalue: value*2})
}
return arr
输出将是

[
  {value: 3, someothervalue: 6},
  {value: 4, someothervalue: 8},
  {value: 5, someothervalue: 10},
  {value: 6, someothervalue: 12}
]

仅供参考:
someothervalue
应该只是证明对象具有多个依赖于每个范围值的值。

您可以删除循环中的“=”,因为大多数人都不希望较大的值包含在内:从Mozilla文档中,您可以使用如下函数生成一个范围:

const range=(start,stop,step=1)=>Array.from({length:(stop-start)/step+1},(u,i)=>start+(i*step));
控制台日志(范围(3,6))
const range=(start,stop,{value,othervalue,steppup})=>Array.from({length:(stop-start)+1},(v,i)=>({[value]:start+i,[othervalue]:(start+i)*steppup});
让结果=范围(3,6,{'value':'myvalue','othervalue':'myothervalue','steppup':2});
让result2=range(4,8,{'value':'myvalue1','othervalue':'myothervalue1','steppup':2});
控制台日志(结果);

console.log(result2)你提到的“FYI”,预期的输出是什么,你能举个例子吗?您编写的代码对我来说非常简单…:-)这个答案更适合作为对帖子的评论;它不会试图解决OP提出的问题。
[
  {value: 3, someothervalue: 6},
  {value: 4, someothervalue: 8},
  {value: 5, someothervalue: 10},
  {value: 6, someothervalue: 12}
]