1

Trying to pull some data out of a returned json object. While testing this is the json object I get.

I'm trying to access the height but can't seem to get it.

Here is the data from chrome expression watcher

testData: Object
  10100832561876234: Array[9]
    0: Object
       height: 2048
       source: "https://fbcdn-sphotos-h-a.akamaihd.net"
       width: 1529
       __proto__: Object
    1: Object
    2: Object
    3: Object
    4: Object
    5: Object
    6: Object
    7: Object
    8: Object
    length: 9
    __proto__: Array[0]
10100856101138364: Array[9]
    0: Object
    1: Object
    2: Object
    3: Object
    4: Object
    5: Object
    6: Object
    7: Object
    8: Object
    length: 9

Here is my code to get the height

testData = jQuery.parseJSON( jsonData );
for (var property in testData) {
      tester = property[0].height;
      alert(tester);
}

Currently I'm getting undefined in my alert

2 Answers 2

4

For loops in JavaScript yield keys, not values.

tester = testData[property][0].height;
Sign up to request clarification or add additional context in comments.

1 Comment

This used to get me all the time. +1 for simplicity
-1

Try this:

var testData = jQuery.parseJSON( jsonData );
for (var property in testData) {
    if (testData.hasOwnProperty(property)) {
         var tester = testData[property][0].height; // or testData[property].height if that's what you need
      alert(tester);
    }
}

1 Comment

Did I miss something? Please comment why I got the downvote so I can learn from the mistake I apparently made.

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.