2

Is there a way in Typescript I can initialize an array with values from n to m without naming all elements?

What I use today:

let lst : number [] = [5,6,7,8];

What I am looking for:

let lst : number [] = [5 .. 8];
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
chris01
  • 10,921
  • 9
  • 54
  • 93
  • No, you should create an utility function for it. – captain-yossarian from Ukraine Dec 03 '21 at 14:14
  • 1
    (Because no, there's no TypeScript-specific syntax for that.) – jonrsharpe Dec 03 '21 at 14:15
  • 1
    As far as I see, the question has not been answered by the posted threat. The answer-threat is about creating an array from 1 ... N, the question was from n ... m. – fonzane Dec 03 '21 at 14:23
  • But also there is still an answer in the threat: Array.from({length: 10}, (_ , i) => i + 1) 10 and 1 are your variables here, 10 being the length you want and 1 the value of the first element. So for your example: let lst: number[] = Array.from({length: 4}, (_ , i) => i + 5) – fonzane Dec 03 '21 at 14:27

0 Answers0