2

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.

1
  • Where is the data coming from (and in what form)? A database? If not, you may want to hard-code it into a JSON file. Commented Apr 29, 2012 at 14:10

4 Answers 4

2

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;
Sign up to request clarification or add additional context in comments.

2 Comments

@ŠimeVidas - d'oh. been using too many different languages lately. thanks.
@Mark Reed - many thanks, this is much simpler than the code I have. Thanks for your comments, very helpful indeed. Regards, Ben.
1

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. */}

1 Comment

Thanks @Kooilnc this is very helpful indeed. Great explanation and eay to understand. Regards, Ben.
0

There were some details I don't know, for example, are you checking for name existence so that, when the name exists,

  1. you add new counts to the existing array?
  2. Replace current array with new array?
  3. Ignore the new information?

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.

Comments

0
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))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.