1

I have looked everywhere for this but nobody seems to use associative arrays in objects. Here is my object:

var player = {
     Level: 1,
     Stats: [{Defense : 5}, {Attack: 1}, {Luck: 3}]
};

I need to access the values of Defense, Attack, and Luck, but how?

I have tried this but it hasn't worked:

player.Stats.Defense
player.Stats.Attack
player.Stats.Luck

Any ideas? Thanks!

P.S. Does it make a difference that I am using jQuery?

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
LukeK
  • 125
  • 1
  • 2
  • 8
  • status is an array of objects, so you need to access each item using positional index – Arun P Johny Dec 30 '13 at 04:16
  • 2
    Are you in control of the code generating that structure? If you are, if would make _way_ more sense to change it such that `Stats` is an object rather than an array of dissimilar objects as in `Stats: {Defense: 5, Attack: 1, Luck: 3}` – Michael Berkowski Dec 30 '13 at 04:16
  • 2
    a more appropriate format will be `Stats: [{ Defense: 5, Attack: 1, Luck: 3 }]` – Arun P Johny Dec 30 '13 at 04:17
  • 1
    possible duplicate of [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Felix Kling Dec 30 '13 at 04:17
  • 1
    *"Does it make a difference that I am using jQuery?"* No. Is there a reason why you want `Stats` to be an array? Using an object with three properties seems to make more sense. – Felix Kling Dec 30 '13 at 04:19
  • I am in control of generating the code. I thought that making it an associative array would be easier, but guess not. Thanks, at least I know how to if it comes up again. – LukeK Dec 30 '13 at 04:19
  • @LukeK: Making it an object (what you called an "associative array") *would* be easier. But that's not what you've done. You've created an array (numerically-based) containing three individual objects. You want: `Stats: {Defense : 5, Attack: 1, Luck: 3}`. – T.J. Crowder Dec 30 '13 at 04:20
  • @LukeK: There are no associative arrays in JS. You can use objects and something similar to associative arrays. But you created an *array of objects*, which is something completely different. – Felix Kling Dec 30 '13 at 04:20
  • "associative array" is kind of a PHP-ish term, and what you have here isn't really a good correlate to a PHP-style assoc array. Build it such that it looks like my earlier comment. Then the examples you tried would work to access them. – Michael Berkowski Dec 30 '13 at 04:21
  • 2
    @ArunPJohny But remove the outer `[]` as it adds no value. `Stats` should itself just be the whole object, not an array containing the object. – Michael Berkowski Dec 30 '13 at 04:21

3 Answers3

4

You've said you're in control of the structure. If so, change it to this:

var player = {
     Level: 1,
     Stats: {Defense : 5, Attack: 1, Luck: 3}
};

Note that Stats is now an object, not an array. Then you access that information the way you tried to, player.Stats.Defense and so on. There's no reason to make Stats an array of dissimilar objects, that just makes your life difficult.

You've used the term "associative array" which makes me think you have a PHP background. That term isn't commonly used in the JavaScript world, to avoid confusion with arrays. "Object," "map," or "dictionary" are the terms usually used, probably in that order, all referring to objects ({}). Probably nine times out of ten, if you would use an associative array for something in PHP, you'd use an object for it in JavaScript (in addition to using objects for the same sort of thing you use objects for in PHP).

P.S. Does it make a difference that I am using jQuery?

No, this is language-level rather than library-level, but it's a perfectly reasonable question.


(Making this a CW answer because it's basically what all the comments on the question are saying.)

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
2

as Stats: [{Defense : 5}, {Attack: 1}, {Luck: 3}] is array of objects, you need to do:

player.Stats[0].Defense
player.Stats[1].Attack
player.Stats[2].Luck
Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
0

Here player.Stats is an array of objects. So you'll have to use index for accessing those objects.

var player = {
     Level: 1,
     Stats: [{Defense : 5}, {Attack: 1}, {Luck: 3}]
};

Use these :

player.Stats[0].Defense
player.Stats[1].Attack
player.Stats[2].Luck
Nishu Tayal
  • 20,106
  • 8
  • 49
  • 101
  • When you find you've posted an answer that's just a duplicate of an earlier answer, it's best to delete your duplicate. – T.J. Crowder Dec 30 '13 at 04:57