1

I am new to working with AJAX and JSON and for a while I haven't have any issue. But now I am using Random User Generator -> http://randomuser.me/

The issue is that after I to the AJAX request, I get the JSON object in response but it's not like all the others I've work on. This one has a value that I can't get on the console log.

Object {results: Array[1]}
  results: Array[1]
    0: Object
      seed: "b68b37c1b5346af"
      user: Object
        SSN: "152-27-4426"
        cell: "(316)-378-6161"
        email: "[email protected]"
        gender: "male"
          location: Object
          city: "the colony"
          state: "georgia"
          street: "4660 w belt line rd"
          zip: "38439"

The problem is that if I am trying to get to the zip code but with the code I got stucked on the second object '0'.

This is my code to retrieve all the data as I've show

$.ajax({
      url: randomuserURL,
      dataType: 'json',
      success: function(data){
        console.log(data);
      }
 });

But if I change the console log to:

$.ajax({
      url: randomuserURL,
      dataType: 'json',
      success: function(data){
        console.log(data.results.[0]);
      }
});

I got an error. Also if I just do it like this

console.log(data.results.0);

Can anyone explain me how to handle this?

0

1 Answer 1

1

It's not

console.log(data.results.[0]);

it's

console.log(data.results[0]);

The . and [ ] operators are two flavors of the same basic idea, so using both at the same time doesn't make sense. When you're trying to access a property that's got a "clean" name that you know in advance (that is, when the property name isn't determined at runtime), you can use the . operator. Otherwise, it's [ ].

In this case, you do know the property name in advance - it's "0". Unfortunately, "0" isn't a valid identifier name, so you can't use ..

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

Comments

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.