-1

I currently have this function to fetch youtube videos:

    function searchYouTube(searchstringYT, white2) 
{
var yt_url = 'http://gdata.youtube.com/feeds/api/videos?q=' + searchstringYT + '&format=5&max-results=1&v=2&alt=jsonc'; 
    $.ajax({
        url : yt_url,
        dataType : "jsonp",
        success: function (json)
        {
            var result= json.data.items; 
                for (i in result)
                {
                    if (result[i].title.toLowerCase().indexOf("live") >= 0)
                    {
                        $(".whiteDiv2").html(""); 
                        $("<h2/>").text("LIVE")
                        .appendTo(white2);
                        $("<h3/>")
                        .text(result[i].title)
                        .appendTo(white2);
                        $("<iframe/>")
                        .attr("src","http://www.youtube.com/embed/"+result[i].id)
                        .css({
                                "width":"620",
                                "height":"400"
                            })
                        .appendTo(white2);
                    }

                }
        }
    });
}

Problem is: I do this with a for loop, but I only need to do it for one video.. My question is: how do I remove the for loop, since I only need to run this loop once?

Also for some weird reason: if I add a else{} to my if(result..) function, the else is always executed, it just skips the above part. With the else left out, it always does the if part though...

1 Answers1

3

If you really want to stop after one iteration, add:

break;

...just before the } closing the loop body.

Note that the order in which the property names are visited in a for-in loop is not defined, and so you'll be picking one "at random." (In fact, it won't be random on any particular implementation, and most implementations visit the keys in the order the properties were added to the object, but it's not specified behavior.)

Of course, if result is an array, you shouldn't be using for-in on it in the first place (at least, not without safeguards). If that's the case, you can remove the loop and use result[0] where you're currently using result[i]. (Assuming the array isn't sparse.) For instance:

if (result[0].title.toLowerCase().indexOf("live") >= 0)
// Here ---^

...and also in the other places you use result[i].

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • First of all: thanks for your reply! Unfortunately I did exactly as you said (remove the for loop, and replace result[i] by result[0] everywhere, but now the code won't work. Any idea why this may happen? – user3162496 Jan 05 '14 at 12:52
  • As T.J. pointed out in another post, [I suggest you read this good explanation about `for-in`](http://blog.niftysnippets.org/2010/11/myths-and-realities-of-forin.html). EDIT. Oh lol, it is ftrom T.J. haha. – Jori Jan 05 '14 at 12:56
  • @user3162496: See the *first* part of my answer. Sounds like `result` isn't an array. – T.J. Crowder Jan 05 '14 at 13:31