Reputation: 6086
I am looking to store the following data structure:
TIMESTAMP | NAME | COUNTS
For each time stamp, there can be multiple names, and each name has a list of counts:
123456 | EXAMPLE1 | 1,2,3,4
| EXAMPLE2 | 4,3,2,1
| EXAMPLE3 | 9,7,4,3
I would like to store the timestamp as an object index so that I can access it when I know the timestamp. This also eliminates any issues with array size limits and defining etc.
What I dont know is how to create a list of "names" for each timestamp, and for each name I would like to store a list of counts.
Can anyone advise how I can create this data structure and access all counts for a specific timestamp and name?
So far I have this:
dataObj[timestamp] = new Array(name,[count]);
But i have 2 questions:
1) How would I test to see if I already have the name?
2) How would I add another name array?
Appreciate any comments.
Regards, Ben.
Upvotes: 2
Views: 177
Reputation: 122908
You can nest objects within objects. So your dataObj
could look like
var dataObj = {},
currentTime = new Date().getTime();
dataObj[currentTime].example1 = [1,2,3,4]; //<== question 2
// later ...
if (dataObj[currentTime] && !dataObj[newTime].example2){ //<== question 1
dataObj[currentTime].example2 = [4,3,2,1]; //<== question 2
}
/*
lets say currentTime was 1335709735241. After the previous
code dataObj contains:
dataObj['1335709735241'].example1 => [1,2,3,4]
dataObj['1335709735241'].example2 -> [4,3,2,1]
*/
Another way of checking for existence of a property in some Object:
if ('someproperty' in someObj) { /* etc. */}
Upvotes: 1
Reputation: 144669
var timeStamp = new Object();
function propCreator(obj, k, arr) {
if (!obj.hasOwnProperty(k)) {
obj[k] = arr;
}
else {
alert("sry. object have property " + k);
}
}
// create new name and array
var arr = [3,4,5,6];
var name = "theName";
// then you can add new new property and its value to the object:
propCreator(timeStamp, name, arr);
alert(JSON.stringify(timeStamp))
Upvotes: 0
Reputation: 6779
There were some details I don't know, for example, are you checking for name existence so that, when the name exists,
The code below does 2. Replace. Try it out http://jsfiddle.net/DrPw6/1/
var data = {};
setNameArray('12345', 'example1', [1,2,3]);
setNameArray(33, 'example2', [4,5,6]);
function setNameArray(timestamp, name, array) {
if (!(timestamp in data)) {
data[timestamp] = {};
}
data[timestamp][name] = array;
}
function nameExists(timestamp, name) {
return (name in data[timestamp]);
}
// to read
document.write(data['12345'].example1 + "<br>");
document.write(data['12345']['example1'] + "<br>");
document.write(JSON.stringify(data));
Note that data.12345.example1
will not work because the key is a number.
Upvotes: 0
Reputation: 95242
Given a timestamp, a name, and a count:
// if we haven't seen this timestamp, create a new object for it
dataObj[timestamp] = dataObj[timestamp] || {};
// if we haven't seen this name yet in this time stamp, create a new array for it
dataObj[timestamp][name] = dataObj[timestamp][name] || [];
// add this count to the array
dataObj[timestamp][name].push(count);
If you get all the counts for a given timestamp/name pair at once, you can combine the last two statements into one:
dataObj[timestamp][name] = countsArray;
Upvotes: 2