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>