Ben
Ben

Reputation: 6086

JavaScript - creating an array within an object

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

Answers (4)

KooiInc
KooiInc

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

Ram
Ram

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

Tina CG Hoehr
Tina CG Hoehr

Reputation: 6779

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.

Upvotes: 0

Mark Reed
Mark Reed

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

Related Questions