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"

4
  • 1
    array[123][1] - assuming you named your var array, don't use capitals for variable names. Commented Nov 22, 2012 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. Commented Nov 22, 2012 at 17:35
  • developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/… Commented Nov 22, 2012 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? Commented Nov 22, 2012 at 20:31

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

8 Comments

I would use [] instead of new Array() above, for the benefit of newbies reading these posts. (stackoverflow.com/questions/885156/…)
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: An Array is an object, and all Array indices are named properties.
@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: 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.
|
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"

Comments

0

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

array[123][1]

Comments

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

4 Comments

@user1689607 Because this explains the problem more explicitly. You're going to downvote it because you liked an earlier version better?
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.
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).
Well, that first line didn't have code formatting at first, so it was easy to overlook.

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.