0

This is silly but I can't figure it out.

I have a simple object in JS:

var objSyntax = [ {"a": "1"},
                  {"b":  "2" },
                  {"c":  "3"} ];

I want to call 1 when theid is equal a, etc.

Here is the notation I tried which did not work:

objSyntax[theid];

What am I doing wrong?

1
  • 4
    You have an Array of Objects. You'll need to iterate the Array, and test each Object to see if it has the "a" key. Or if the keys are guaranteed to be unique, you should probably make a single Object var objSyntax = {"a":"1", "b":"2", "c":"3"} Commented Jul 23, 2013 at 16:33

2 Answers 2

2

you could change your object to reflect something like:

var objSyntax = {"a": "1",
                  "b":  "2" ,
                  "c":  "3"};
objSyntax[theId];

or iterate through the array of objects you have posted:

var objSyntax = [ {"a": "1"},
                  {"b":  "2" },
                  {"c":  "3"} ];
Sign up to request clarification or add additional context in comments.

2 Comments

Perfect! Your first solution worked great, and it's much simpler than the second option. Thanks!
Of course, you have an array of objects and not an object as you claim in your question.
-1

With how you have it set up right now, you would access it like this

objSyntax[0][theId]

1 Comment

That will only work if theId contains a. If it's b he needs to do objSyntax[1][theId], and so on.

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.