0

Hi guys so I was wondering if it was possible to search content in a Json object with a variable?

Example:

var username = "name";

The json object Im getting from an api is in the following format:

{"name":{"id":224463,"name":"name","profileIconId":715,"revisionDate":1465905397000}}

The username is obviously different for everyone. I want to be able to get the id of the user and display it when the user enters their username in a text field.

 HTTP.get(url,function(error,result){
    var username ="somename";
    console.log(result.username.id);

    });

That code above gives me undefined but if I put somename where username is then it works fine.

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
  • 1
    [There's no such thing as a "JSON object"](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/) – Andreas Jun 16 '16 at 07:54

1 Answers1

0

Use bracket notation to access property using a variable

console.log(result[username].id)
//               -^-      -^-
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188