Last active
October 6, 2017 11:10
-
-
Save alizhdanov/9c8d44c4cf145a3ed2615e1884c52b58 to your computer and use it in GitHub Desktop.
Generate array sequence (ES6)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[...Array(N).keys()] // will generate array sequence with N length | |
// example | |
[...Array(5).keys()] // [0,1,2,3,4] | |
// Don't hesitate to use map if you need array started from some specific index | |
[...Array(5).keys()].map(i => i+1) // [1,2,3,4,5] | |
// Also You can create some fancy util function | |
const getSequnce = (n, offset=0) => { | |
return [...Array(n).keys()].map(i => i + offset) | |
} | |
getSequnce(5) // [0, 1, 2, 3, 4] | |
getSequnce(5,1) // [1, 2, 3, 4, 5] | |
getSequnce(5,6) // [6, 7, 8, 9, 10] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment