0

I am retrieving a json array of information using Youtube's api. I want to loop through the "items" array which is within the entire array to access each item's video ID.

results = {
"kind": "youtube#searchListResponse",
"etag": "\"bvxF-DWHx1toJotsdJBeCm43SLs/Z5vAcycCC4VZd3cPfwzTWZ_FrUQ\"",
"nextPageToken": "CAUQAA",
"pageInfo": {
    "totalResults": 1000000,
    "resultsPerPage": 5
},
"items": [
{
    "id": {
    "kind": "youtube#video",
    "videoId": "TvyWRevLG5I"
    }
},
{   
    "id": {
        "kind": "youtube#video",
        "videoId": "P8iKcdh5Ims"
    }
},
{
    "id": {
        "kind": "youtube#video",
        "videoId": "tiRZ7YQKUgQ"
    },
},
{
   "id": {
        "kind": "youtube#video",
        "videoId": "mENb0jX-SD8"
   }
},
{
   "id": {
        "kind": "youtube#video",
        "videoId": "B34hQieOXZs"
   }
}
]
}

This is my jquery code to loop through:

$.each(results.items, function(i, item) {
    alert(items[i].id.videoId);
});

I am getting the error "Cannot read property 'length' of undefined" for some reason. Also when I try getting the length (results.items.length)to use a normal forloop, I get the same error.

2
  • You must be using Ajax .please put your entire jqury function here Commented May 24, 2014 at 23:00
  • Works for me (after a small change)? jsfiddle.net/Nc8VS Commented May 24, 2014 at 23:01

1 Answer 1

1

You should fix a little bit your each function, because you reference to wrong object:

$.each(results.items, function(i, item) {
    console.log(item.id.videoId);
});

Fiddle

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.