Create an array of characters from specified range
If you're using ES6, you can generate a sequence using Array.from() by passing in an array-like object for the length of the range, and a map function as a second argument to convert the array key of each item in the range into a character using String.fromCharCode():
Array.from({ length: 26 }, (_, i) => String.fromCharCode('A'.charCodeAt(0) + i));
You can also use the Array constructor (note: ES6 allows constructors to be invoked either with a function call or with the new
operator) to initialize an array of the desired default length, fill it using Array.fill(), then map through it:
Array(26).fill().map((_, i) => String.fromCharCode('A'.charCodeAt(0) + i));
The same can be accomplished with the spread operator:
[...Array(26)].map((_, i) => String.fromCharCode('A'.charCodeAt(0) + i));
The above three examples will return an array with characters from A to Z. For custom ranges, you can adjust the length and starting character.
For browsers that don't support ES6, you can use babel-polyfill or core-js polyfill (core-js/fn/array/from).
If you're targeting ES5, I would recommend the Array.apply solution by @wires which is very similar to this one.
Lastly, Underscore/Lodash and Ramda have a range() function:
_.range('A'.charCodeAt(0), 'Z'.charCodeAt(0) + 1).map(i => String.fromCharCode(i));
Javascript doesn't have that functionality natively. Below you find some examples of how it could be solved:
Normal function, any characters from the base plane (no checking for surrogate pairs)
function range(start,stop) { var result=[]; for (var idx=start.charCodeAt(0),end=stop.charCodeAt(0); idx <=end; ++idx){ result.push(String.fromCharCode(idx)); } return result;};range('A','Z').join();
The same as above, but as a function added to the array prototype, and therefore available to all arrays:
Array.prototype.add_range = function(start,stop) { for (var idx=start.charCodeAt(0),end=stop.charCodeAt(0); idx <=end; ++idx){ this.push(String.fromCharCode(idx)); } return this;};[].add_range('A','Z').join();
A range from preselected characters. Is faster than the functions above, and let you use alphanum_range('A','z')
to mean A-Z and a-z:
var alphanum_range = (function() { var data = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'.split(''); return function (start,stop) { start = data.indexOf(start); stop = data.indexOf(stop); return (!~start || !~stop) ? null : data.slice(start,stop+1); };})();alphanum_range('A','Z').join();
Or any character from the ascii range. By using a cached array, it is faster than the functions that build the array every time.
var ascii_range = (function() { var data = []; while (data.length < 128) data.push(String.fromCharCode(data.length)); return function (start,stop) { start = start.charCodeAt(0); stop = stop.charCodeAt(0); return (start < 0 || start > 127 || stop < 0 || stop > 127) ? null : data.slice(start,stop+1); };})();ascii_range('A','Z').join();
var chars = [].concat.apply([], Array(26)) .map(function(_, i) { return String.fromCharCode(i+65); }) .join();
The .map
function could be a function generator that could be used for different character sets.
function charRange(start) { var base = start.charCodeAt(0); return function(_, i) { return String.fromCharCode(i + base); };}
And you may also want to create a "full" Array helper.
function fullArray(len) { return [].concat.apply([], Array(len)); }
Then use them like this.
var chars = fullArray(26).map(charRange("A")) .join();