3

Updated: Using return false in the if statement can only return the "No" messages. All the "Yes" messages are skipped. How can I get the Yes messages to show up as well? Here's the updated Example.

Example (Single object looping over a list of keywords)

Example 2 (An array of objects looping over a list of keywords)

I can't get the second example to skip to next keyword once it checked that the keyword exists in the array of return objects. As you click through the alert messages, you can see the Yes messages are overwritten by No messages because it isn't able to move on to the next keyword and continue to check against the other objects which do not contain the keyword.

return true; doesn't seem to work. Can anyone show me how to skip to the next loop once the keyword is found in the returned object?

 $.ajax({
    url: "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20json%20where%20url%20%3D%20%22http%3A%2F%2Fgoo.gl%2FSXYySV%22&format=json&diagnostics=true&callback=",
    success: function (data) {

        var keywords = ["Germany","England"];

    $(data.query.results.json.json).each(function (index, item) {
      var nation1 = item.nation1,
            nation2 = item.nation2,
            title = item.title;
      var objects = [nation2,nation1,title];
      $.each(objects,function(i,object){ 
        $.each(keywords,function(i,keyword){ 
            alert(keyword)
            if ($.inArray(object,keywords)!=-1) 
            {   
                $('.'+ title+'_'+keyword).html(title+' contains '+keyword)
                return true;
            } 
            else 
            {
                $('.'+ title+'_'+keyword).html(title+' does not have '+keyword)
            }  
        });   
       });
     }); 
    }

});

HTML:

<div class="A_Germany"></div>
<div class="B_Germany"></div>
<div class="C_Germany"></div>
<div class="A_England"></div>
<div class="B_England"></div>
<div class="C_England"></div>

JSON

[{"title":"A","nation1":"Turkey","nation2":"Germany"},{"title":"B","nation1":"France","nation2":"Japan"},{"title":"C","nation1":"Italy","nation2":"England"}]

Expected Output should be

<div class="A_Germany">Yes</div>
<div class="B_Germany">No</div>
<div class="C_Germany">No</div>
<div class="A_England">No</div>
<div class="B_England">No</div>
<div class="C_England">Yes</div>
RedGiant
  • 4,444
  • 11
  • 59
  • 146
  • Returning skips the item as expected. http://jsfiddle.net/jU6vj/Do you need to skip the *outer* loop `$.each(objects`? – Popnoodles Jun 17 '14 at 19:09
  • @Popnoodles , please check [this example](http://jsfiddle.net/9mwCG/23/), I can only get the No message to show up. – RedGiant Jun 17 '14 at 19:22
  • @AndrewArnold, I've tried `return false` in the if condition, but it can only get the No message to show up. – RedGiant Jun 17 '14 at 19:25
  • Please show the expected response. – Popnoodles Jun 17 '14 at 19:26
  • @AndrewArnold, yes someone is locking my thread too quickly. The suggested thread isn't really helpful at all. – RedGiant Jun 17 '14 at 19:27
  • 4
    **This is not a duplicate of that question.** – Popnoodles Jun 17 '14 at 19:28
  • It's not clear which loop you want to escape from. – Popnoodles Jun 17 '14 at 19:30
  • Well, don't I feel silly. `return false` *is* supposed to break. Not sure why it wouldn't work here. – Andrew Jun 17 '14 at 19:30
  • @Popnoodles I want it to move on to the next loop and next keyword once the keyword is found in the array and it returns the Yes message. I think my updated example is close, isn't it? It just doesn't show the Yes message. – RedGiant Jun 17 '14 at 19:32
  • @Popnoodles, I've updated the thread with the JSON data and the expected results. – RedGiant Jun 17 '14 at 19:41
  • @Popnoodles, basically, whenever the keyword is found in either `item.nation1` or `item.nation2`, a Yes message should be returned and it will move on to the next loop – RedGiant Jun 17 '14 at 19:43
  • 1
    I think your logic structure is a little wrong—you want to break out of the outer loop, but the `return` only breaks out of the inner one. Try playing around with this: http://jsfiddle.net/GU44A/ – Andrew Jun 17 '14 at 19:56
  • 2
    You could use a normal loop and then use `break` or `continue`, instead of `$each`... – vsync Jun 17 '14 at 20:05
  • @RedGiant I updated my answer because there was a typo. It worked but what I thought I'd done wasn't what was happening. – Popnoodles Jun 17 '14 at 20:17
  • @Popnoodles, thank you very much. It works! That's the ideal output – RedGiant Jun 17 '14 at 20:21
  • 1
    @epascarello If you don't care about the question, just don't post or give misleading information and dismiss other people threads out of hand without reading what is actually asked. The suggested thread doesn't solve this actual problem. – RedGiant Jun 18 '14 at 13:55

1 Answers1

2

Ok, forgive me for renaming your variables - I had to give them context.

Instead of looping per team for all items, check each specific item once against each team, e.g. A_England only checks for A and England not A but not England

var teams = ["Germany","England"];
$.ajax({
    url: "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20json%20where%20url%20%3D%20%22http%3A%2F%2Fgoo.gl%2FSXYySV%22&format=json&diagnostics=true&callback=",
    success: function (data) {
        $(data.query.results.json.json).each(function (index, item) {
            // make an array of the nations
            var arr = [item.nation1, item.nation2];
            // check for a team in this game
            $.each(teams, function(i, team){
                    team_index = $.inArray(team, arr);
                    if (team_index > -1){
                        $('.'+ item.title + '_'+ team).html("Yes");
                    }else{
                        $('.'+ item.title + '_'+ team).html("No");
                    }
            });

        }); 
    }
});

http://jsfiddle.net/9mwCG/26/

I've also checked that it works when you add more teams. http://jsfiddle.net/9mwCG/28/

Popnoodles
  • 28,090
  • 2
  • 45
  • 53