itamar
itamar

Reputation: 3967

Get Value from Object using another value

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?

Upvotes: 0

Views: 55

Answers (2)

John
John

Reputation: 6278

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

objSyntax[0][theId]

Upvotes: -1

dm03514
dm03514

Reputation: 55972

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"} ];

Upvotes: 2

Related Questions