My JSON object contains data organized like this:
"time1": {
"item1": "val1",
"item2": "val2"
},
"time2": {
"item1": "val3",
"item2": "val4"
}
...
The time*
values represent dates in milliseconds and are sorted in sequence.
Now, I need to search for an item based on the time key. If the key doesn't exist, I must find the nearest value. There are thousands of entries in the object and I'm considering using binary search, but I'm unsure about how to implement it.
The traditional method of calculating the middle position middle = (right+left)/2
cannot be used because the middle value might be undefined
.
Due to the fixed structure of my data, I am unable to utilize a binary tree.
Any insights or suggestions would be greatly appreciated.