1

I got a structure like this:

var Array = new Array(3);

Array["123"] = ["a","b","c"];

Array["456"] = ["d","e","f"];

Array["789"] = ["g","h","i"];

for example, how do I get "b"

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • 1
    `array[123][1]` - assuming you named your `var array`, don't use capitals for variable names. – Tomasz Nurkiewicz Nov 22 '12 at 17:35
  • Take the syntax you used to assign to the Array, use it to get the Array at that index, and the same syntax to get an item from the nested Array. – I Hate Lazy Nov 22 '12 at 17:35
  • https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/Member_Operators#Bracket_notation – Liviu T. Nov 22 '12 at 17:35
  • what are you trying to do? "Array" is a constructor for arrays and probably can't be used as a variable name. if it were a variable name Array["123"] is how you'd reference member "123" of an object, this will work as arrays are objects, but it's unlikely it's what you actually want. maybe describing what you want and saying which language you're coming from would help? – dtudury Nov 22 '12 at 20:31

4 Answers4

1
var a = new Array();   
a["123"] = ["a","b","c"];
a["456"] = ["d","e","f"];
a["789"] = ["g","h","i"];
b = a["123"][1];

sample :) http://jsbin.com/agolef/1/edit

Boris Gappov
  • 2,483
  • 18
  • 23
  • I would use `[]` instead of `new Array()` above, for the benefit of newbies reading these posts. (http://stackoverflow.com/questions/885156/whats-wrong-with-var-x-new-array) – btiernay Nov 22 '12 at 17:43
  • This doesn't make explicit the fact that the user is treating a as an object, adding named properties instead of indices. I think explaining this is more important than explaining how to access indices, which is trivial. – Asad Saeeduddin Nov 22 '12 at 17:43
  • @Asad: An Array *is* an object, and all Array indices are named properties. – I Hate Lazy Nov 22 '12 at 17:45
  • @user1689607 I already know this. Read my comment again. I said he is *treating* `a` as an object by adding named properties instead of numeric indices (which is a misuse of an `Array` type object). For example, checking a.length would yield 0, which could be unexpected for a new user. All array indices are named properties, but not all named properties are array indices. – Asad Saeeduddin Nov 22 '12 at 17:49
  • @Asad: Right, and OP was only using indices. All the properties added in the OP would be used as indices of the Array. The `.length` would *not* be `0`. It would be `790`. – I Hate Lazy Nov 22 '12 at 17:54
  • ... *all* properties of JavaScript objects *(including Array objects)* are ultimately strings. – I Hate Lazy Nov 22 '12 at 17:59
  • @user1689607 In this specific case, yes, but I think the fact that he is using string literals instead of numeric literals is a code smell that betrays a deeper misunderstanding. – Asad Saeeduddin Nov 22 '12 at 18:00
  • @Asad: There's no doubt that there's some misunderstanding. The biggest clue is not knowing how to access members of an Array. – I Hate Lazy Nov 22 '12 at 18:00
1
a["123"][1]; // yields "b"
a[123][1]; // also yields "b"

Indexing an array with a string is probably not what you meant to do.

var a = new Array(3);

a["123"] = ["a","b","c"];  // "123" causes the array to expand to [0..123]
a["123"][1]; // yields "b"
a[123] = ["a","b","c"];  // this has better performance and is idiomatic javascript.
a[123][1]; // also yields "b"
a["456"] = ["d","e","f"];
a["789"] = ["g","h","i"];

If you want to use an object as a map instead, try this:

a = new object()
a["123"] = ["a","b","c"];
a["123"][1]; // yields "b"
agent-j
  • 27,335
  • 5
  • 52
  • 79
0

Array is a native constructor. Using a new object that doesn't add properties to the native object:

var obj = {};

obj["123"] = ["a","b","c"];

obj["456"] = ["d","e","f"];

obj["789"] = ["g","h","i"];

obj["123"][1]; // "123"

What your code was doing was adding a bunch of properties to the native Array, (which is a function object that makes array objects). For more on the difference between arrays and other objects, see this question

Community
  • 1
  • 1
Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139
  • @user1689607 Because this explains the problem more explicitly. You're going to downvote it because you liked an earlier version better? – Asad Saeeduddin Nov 22 '12 at 17:41
  • I didn't DV you, but I did see that you had 2 votes before you changed it. One was removed and then you got the DV. – I Hate Lazy Nov 22 '12 at 17:42
  • The code was not adding properties to the `Array` constructor. It was overwriting the constructor with an Array instance *(assuming the code was run in the global scope)*. – I Hate Lazy Nov 22 '12 at 17:58
  • Well, that first line didn't have code formatting at first, so it was easy to overlook. – I Hate Lazy Nov 22 '12 at 18:02
0

Use something like this (you don't need the quotes):

array[123][1]
DarkAjax
  • 15,955
  • 11
  • 53
  • 65