const generateArray =
(aroundNumber) => [...Array(9).keys()].map(i => i + aroundNumber - 4);
generateArray(240) // [236, 237, 238, 239, 240, 241, 242, 243, 244 ];
Explanation:
Array(9)
is creating an array of 9 elements
keys()
methods would return 1,2,3,4,5,6,7,8,9
map
transforms it to the range you need - it takes the i
(the number you are transforming to something else) and adds an offset to it (in this case aroundNumber - 4
because your array is starting 4 elements below the number)
Also, as @Estradiaz mentioned in the comments, you can use Array.from
method
const generateArray =
(aroundNumber) => Array.from({length: 9}, (v, i) => i + aroundNumber - 4);
You can read more about the Array.from()
method here