Since it looks like the array is sorted, why don't you use binary search algorithm. I put together a jsfiddle using binary search and match criteria assumes inclusive "from". In other words 6 would match 9 and not 8.
ranges = [
{from: .015625, to: .0234375},
{from: .0234375, to: .046875},
{from: .046875, to: .09375},
{from: .09375, to: .1875},
{from: .1875, to: .375},
{from: .375, to: .75},
{from: .75, to: 1.5},
{from: 1.5, to: 3},
{from: 3, to: 6},
{from: 6, to: 12},
{from: 12, to: 24},
{from: 24, to: 48}
];
//A function that can build an array of ranges
//by doubling the seed... This looks to produce a
//different results than your ranges as
// .0234375 is not twice .015625.
var buildRanges = function (seed, maxIndex) {
var tmp = [];
var curr = 0;
var from = seed;
var to = 2 * from;
while (curr <= maxIndex) {
tmp.push({from: from, to: to});
from = to;
to = 2 * from;
curr++;
}
return tmp;
}
var findIndex = function (x) {
var min = 0;
var max = ranges.length - 1
var mid;
while (min <= max) {
mid = parseInt((max + min) / 2);
//Assume "from" field is inclusive
if (x >= ranges[mid].from && x < ranges[mid].to) {
return mid;
}
//We know that maximum must be adjusted below mid
else if (x < ranges[mid].from) {
max = mid - 1;
}
//Else we must move up the min
else {
min = mid + 1;
}
}
}
alert(findIndex(.22)); //4
alert(findIndex(6)); //9
alert(findIndex(12)); //10
alert(findIndex(.9)); //6
24/(2^k)for increasing values ofk, except for that lastfromentry, which would be0.01171875.0.015625 != 0.0234375/2. If this was just a mistake then we can get the index usinglg(n/24)or somesuch.24/(2^(11-k))but the answer must be more generic to be useful for other types of (regular or not) ranges.