This question already has answers here:
Create array of all integers between two numbers, inclusive, in Javascript/jQuery [duplicate]
                                
                                    (15个答案)
                                
                        
                                在4个月前关闭。
            
                    
我有一个包含两个元素的数组[5,50]。我希望此数组迭代为[5,6,7,......,49,50]。

我尝试下面的代码。但是没有按我的期望工作。

function All(arr) {
  let newArry = [];
  for(let i = arr[0]; i < arr[1]; i++ ) {
    newArry[i] = newArry.push(i);
  }
  return newArry;
}

console.log(All([5, 50]));

最佳答案

像这样删除newArry[i] =



function All(arr) {
    let newArry = [];
    for(let i = arr[0]; i < arr[1]; i++ ) {
        newArry.push(i);
    }
    return newArry;
}
console.log(All([5, 50]));

10-07 13:27