產生一個整數數組,並用從起始數字開始到結束數字結束的連續值填充它。
function range(start, end) { const rangeArray = Array.from( {length: Math.ceil(end - start + 1)}, (_, i) => start + i); return rangeArray; } console.log(range(10, 23)); console.log(range(5, 12)); console.log(range(89, 93)); console.log(range(42, 51)); console.log(range(73, 80));
> [10, 11, 12, 13, 14, 15,16, 17, 18, 19, 20, 21,22, 23] > [5, 6, 7, 8, 9, 10, 11, 12] > [89, 90, 91, 92, 93] > [42, 43, 44, 45, 46, 47, 48, 49, 50, 51] > [73, 74, 75, 76, 77, 78, 79, 80]
以上是在 JavaScript 產生數字範圍的詳細內容。更多資訊請關注PHP中文網其他相關文章!