Consider the following scenario:
const list = {
1: "a",
10: "b",
20: "c",
30: "d",
40: "e"
};
const value = 15;
I am looking for an efficient way to compare the 'value' against the keys in the object and retrieve the corresponding value from the smaller range. In this case, the expected result would be b
as 15 falls between 10 and 20.
This was my initial approach:
for(var i=0; i < keys.length; i++){
const item = parseInt(keys[i],10);
if (item == keys[i]) {
return keys[i];
}
}
However, I realized that this method is not very efficient...