2

I want to make a structure like this:

var myThings = {
    thing1: {
        ['24-04-12', '90'],
        ['25-04-12', '90'],
        ['26-04-12', '90']
    },
    thing2: {
        ['24-04-12', '10'],
        ['25-04-12', '30'],
        ['26-04-12', '210']
    }
}

(There are 4 things, hardcoded).

The arrays in each object will be added in a loop, so my code looks like this:

var myThings = {"thing1":{}, "thing2":{}};

I then can't work out how to push my arrays into the object.

Am I being dumb, or is this not the way to do this?

Thanks a lot!

Rich Bradshaw
  • 71,795
  • 44
  • 182
  • 241
  • That's not the way to do it. You can't have "loose" arrays in an object. You should use an array instead. – JJJ Mar 31 '12 at 18:06
  • How can I structure it - later I want to loop through the thing1,thing2 etc and read out the date and prices from the internal arrays. What structure would you use? – Rich Bradshaw Mar 31 '12 at 18:07
  • An array of arrays: `var myThings = {"thing1":[], "thing2":[]};` – Felix Kling Mar 31 '12 at 18:07
  • Just use an array: `thing1: []`. Add more stuff there with `myThings.thing1.push( [ another, array ] )` and retrieve them by looping through the array. – JJJ Mar 31 '12 at 18:09

3 Answers3

5

You need to use an Array instead an Object there. Like

var myThings = {
    thing1: [
        ['24-04-12', '90'],
        ['25-04-12', '90'],
        ['26-04-12', '90']
    ],
    thing2: [
        ['24-04-12', '10'],
        ['25-04-12', '30'],
        ['26-04-12', '210']
    ]
}

Now since thing1 and thing2 are Arrays you can just use Array.prototype.push to push new Arrays into that Array.

Object.keys( myThings ).forEach(function( thing ) {
    myThings[ thing ].push( ['01-01-12', '42'] );
});

That code would add ['01-01-12', '42'] to all current things in that object.

Disclaimer: The above code contains ES5. You need a browser which supports that or a Shim to emulate

jAndy
  • 231,737
  • 57
  • 305
  • 359
  • Which bit is ES5 - the `keys` method? – Rich Bradshaw Mar 31 '12 at 18:12
  • @RichBradshaw: `Object.keys` and `Array.prototype.forEach`. There are lots of shims available which emulate that methods for pretty old'ish browser. Also, its very simple to write in ES3 aswell. – jAndy Mar 31 '12 at 18:13
  • What do I need to read to see why I can't put the arrays in an object? Seems I'm missing a bit of fundamn – Rich Bradshaw Apr 01 '12 at 07:36
  • @Rich: Objects are basically `key => value` pairs. In your case, you would not have a key: `{[...], [...]}`. How do you expect to access the arrays? If you want to read up on the basics, have a look at the [MDN JavaScript Guide](https://developer.mozilla.org/en/JavaScript/Guide). – Felix Kling Apr 01 '12 at 10:26
3

I'd do:

//create your array dynamically
var array1 = [['24-04-12', '90'],
        ['25-04-12', '90'],
        ['26-04-12', '90'] ];

var myThings = {
    thing1: array1
        ,
    thing2: array1
}

This way, the properties of your object are arrays.

Mat
  • 202,337
  • 40
  • 393
  • 406
Nicola Peluchetti
  • 76,206
  • 31
  • 145
  • 192
0

Instead of your inner items being objects, you should make them arrays. Objects must exist in key-value pairs, which does not make sense for your usage. Arrays are also key-value pairs, but the key is simply the index number.

var myThings = {
    thing1: [ // thing1 is now an array of arrays
        ['24-04-12', '90'],
        ['25-04-12', '90'],
        ['26-04-12', '90']
    ],
    thing2: [ // thing2 is now an array of arrays
        ['24-04-12', '10'],
        ['25-04-12', '30'],
        ['26-04-12', '210']
    ]
}; // don't forget this semicolon...

You would then access the inner-most values like this:

myThings.thing1[0][1]; // gives you back '90'
myThings.thing2[2][0]; // gives you back '210'

If you want to iterate over the myThings object, use a for...in loop. If you want to iterate over any of the interior arrays, use a regular for loop. As long as you do NOT use for...in on the arrays :)

Community
  • 1
  • 1
jbabey
  • 45,965
  • 12
  • 71
  • 94