I have JS object like this
{
"100": {.. data },
"200": {.. data },
"500": {.. data },
"1000": {.. data }
/* so on */
}
How to search key with any number like
- if search number is between 0 to 99 then it will return false,
- if search number is between 100 to 199 then it will return key 100 and its value
- if 500 to 999 then return key 500 and its value
- if 1000+ then key as 1000 and its value
here is my code
function find(num, obj)
{
let keys = Object.keys(obj);
result = keys.concat(Number.MAX_SAFE_INTEGER).filter(key => {
return num <= key;
}).shift();
if(result === Number.MAX_SAFE_INTEGER) {
result = keys.pop();
}
return result;
}
console.log( find(125, data) )
it's returning 200 instead of 100
return num <= key;will return where num is less than or equal to key...ie the next key, not the previous one.