This is kinda hard to phrase in a single line questions, but I'm looking for some advice/best practices for structuring data and writing a function in Javascript.
I have several items that change status regularly. My data contains an itemID, timestamp, and status. I am currently structuring it as an array of objects (for each item), with a history priority that contains the timestamps and status. (see below).
I'm looking for a function that will allow me to easily get the status of each object at a given time using the most recent past update. I'm not sure if my data structure will allow for this, or if it does, how to write the function. (for this example I'm going to shorten my timestamps to a 4 digit number)
var items = [
{ id: 1,
history: {1234: 'open', 1256: 'in-use', 1289: 'reset', 1293: 'open'},
{ id: 2,
history: {1230: 'open', 1290: 'in-use'},
{ id: 3,
history: {1238: 'open', 1241: 'in-use', 1251: 'reset'}
]
I'd like to be able to have a function like this:
getStatus(1260);
and get back
{1: 'in-use', 2: 'open', 3: 'reset'}
Each id, with the status it was in at the time passed in based on the most recent history record prior to the queried time.
I am not at all attached to this data structure. I also tried having the history an array of objects containing the time and the status, but that means I have to loop through the entire array every time. My biggest problem is that my head is pushing me to an SQL method for doing this, but I'm stuck in client-side Javascript...
My questions: What is the best data structure for this? and How would I go about writing my getStatus() function?
Thanks!