Skip to content

Instantly share code, notes, and snippets.

@atomize
Created October 9, 2018 18:30
Show Gist options
  • Save atomize/0775b90e9880bf0ad461e1c00d4e13bd to your computer and use it in GitHub Desktop.
Save atomize/0775b90e9880bf0ad461e1c00d4e13bd to your computer and use it in GitHub Desktop.
Generate range using Array.from() - with alphabet example.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from#Examples#Sequence_generator_(range)
const range = (start, stop, step) => Array.from({ length: (stop - start) / step }, (_, i) => start + (i * step));
// Generate numbers range 0..4
range(0, 5, 1);
// [0, 1, 2, 3, 4]
// Generate the alphabet using Array.from making use of it being ordered as a sequence
range('A'.charCodeAt(0), 'Z'.charCodeAt(0) + 1, 1).map(x => String.fromCharCode(x));
// ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment