V jazyku PHP môžete...
range(1, 3); // Array(1, 2, 3)
range("A", "C"); // Array("A", "B", "C")
To znamená, že existuje funkcia, ktorá umožňuje získať rozsah čísel alebo znakov odovzdaním hornej a dolnej hranice.
Existuje na to niečo zabudované v JavaScripte? Ak nie, ako by som to mal implementovať?
Funguje pre znaky a čísla, dopredu alebo dozadu s voliteľným krokom.
var range = function(start, end, step) {
var range = [];
var typeofStart = typeof start;
var typeofEnd = typeof end;
if (step === 0) {
throw TypeError("Step cannot be zero.");
}
if (typeofStart == "undefined" || typeofEnd == "undefined") {
throw TypeError("Must pass start and end arguments.");
} else if (typeofStart != typeofEnd) {
throw TypeError("Start and end arguments must be of same type.");
}
typeof step == "undefined" && (step = 1);
if (end < start) {
step = -step;
}
if (typeofStart == "number") {
while (step > 0 ? end >= start : end <= start) {
range.push(start);
start += step;
}
} else if (typeofStart == "string") {
if (start.length != 1 || end.length != 1) {
throw TypeError("Only strings with one character are supported.");
}
start = start.charCodeAt(0);
end = end.charCodeAt(0);
while (step > 0 ? end >= start : end <= start) {
range.push(String.fromCharCode(start));
start += step;
}
} else {
throw TypeError("Only string and number types are supported");
}
return range;
}
[jsFiddle][1].
Ak je rozšírenie natívnych typov vašou záležitosťou, potom ho priraďte k Array.range
.
var range = function(start, end, step) {
var range = [];
var typeofStart = typeof start;
var typeofEnd = typeof end;
if (step === 0) {
throw TypeError("Step cannot be zero.");
}
if (typeofStart == "undefined" || typeofEnd == "undefined") {
throw TypeError("Must pass start and end arguments.");
} else if (typeofStart != typeofEnd) {
throw TypeError("Start and end arguments must be of same type.");
}
typeof step == "undefined" && (step = 1);
if (end < start) {
step = -step;
}
if (typeofStart == "number") {
while (step > 0 ? end >= start : end <= start) {
range.push(start);
start += step;
}
} else if (typeofStart == "string") {
if (start.length != 1 || end.length != 1) {
throw TypeError("Only strings with one character are supported.");
}
start = start.charCodeAt(0);
end = end.charCodeAt(0);
while (step > 0 ? end >= start : end <= start) {
range.push(String.fromCharCode(start));
start += step;
}
} else {
throw TypeError("Only string and number types are supported");
}
return range;
}
console.log(range("A", "Z", 1));
console.log(range("Z", "A", 1));
console.log(range("A", "Z", 3));
console.log(range(0, 25, 1));
console.log(range(0, 25, 5));
console.log(range(20, 5, 5));
Array.range= function(a, b, step){
var A= [];
if(typeof a== 'number'){
A[0]= a;
step= step || 1;
while(a+step<= b){
A[A.length]= a+= step;
}
}
else{
var s= 'abcdefghijklmnopqrstuvwxyz';
if(a=== a.toUpperCase()){
b=b.toUpperCase();
s= s.toUpperCase();
}
s= s.substring(s.indexOf(a), s.indexOf(b)+ 1);
A= s.split('');
}
return A;
}
Array.range(0,10);
// [0,1,2,3,4,5,6,7,8,9,10]
Array.range(-100,100,20);
// [-100,-80,-60,-40,-20,0,20,40,60,80,100]
Array.range('A','F');
// ['A','B','C','D','E','F')
Array.range('m','r');
// ['m','n','o','p','q','r']
Štandardný Javascript nemá zabudovanú funkciu na generovanie rozsahov. Viaceré javascriptové frameworky pridávajú podporu pre takéto funkcie, alebo ako už uviedli iní, vždy si môžete vytvoriť vlastnú.
Ak by ste si to chceli ešte raz overiť, definitívnym zdrojom informácií je norma ECMA-262.