Skip to content

Latest commit

 

History

History
10 lines (8 loc) · 376 Bytes

initialize-array-with-range.md

File metadata and controls

10 lines (8 loc) · 376 Bytes

Initialize array with range

Use Array(end-start) to create an array of the desired length, Array.map() to fill with the desired values in a range. You can omit start to use a default value of 0.

const initializeArrayRange = (end, start = 0) =>
  Array.apply(null, Array(end - start)).map((v, i) => i + start);
// initializeArrayRange(5) -> [0,1,2,3,4]