Javascript Objects and keys

58 views Asked by At

The following command shows the properties of an Object.

var keys = Object.keys(result);

Output: [requester.client.id,request.id]

When I try to print an alert(result[request.id]) or alert(result.request.id) I dont get the values. Is there something I am missing?

2

There are 2 answers

0
Pavlo On BEST ANSWER

In JavaScript objects keys are strings, though they can have periods. What you probably getting as the output is ['requester.client.id','request.id'], so it should be accessed as result['requester.client.id'].

0
apsillers On

Your result object has properties named "requester.client.id" and "request.id".

You need to do alert(result["request.id"]).

result[request.id] does not work because request here is treated as a variable name, and you probably have no variable named request.

result.request.id is closer, but it also fails because the property name has a period in it, so the parser treats this as the the id property of the request property of result.