It's pretty hard to put in a single line of questions, but I'm looking for some recommendations / best practices for structuring data and writing functions in Javascript.
I have several elements that regularly change status. My data contains ItemID, timestamp and status. I am currently structuring it as an array of objects (for each element), with a history priority that contains timestamps and status. (see below).
I am looking for a function that will allow me to easily get the status of each object at a given time using the latest latest update. I am not sure if my data structure will allow this, or if this happens, how to write a function. (for this example, I'm going to reduce the 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 would like to have a function like this:
getStatus(1260);
and come back
{1: 'in-use', 2: 'open', 3: 'reset'}
Each identifier with the status in which it was at the time that passed in accordance with the most recent historical record before the requested time.
I am not tied to this data structure. I also tried to have a history of an array of objects containing time and status, but that means that I need to loop through the entire array every time. My biggest problem is that my head is pushing me towards the SQL method for this, but I'm stuck on client-side Javascript ...
My questions: What is the best data structure for this? and how would I start writing my getStatus () function?
Thanks!