Simplifying AJAX success code Please take a look at this Fiddle.

Is there a better way of building a table with titles in the first cell of each row, like this:

&lt;div id="area"&gt;
 &lt;table&gt;
  &lt;thead&gt;
   &lt;tr&gt;
    &lt;th&gt;&lt;/th&gt;
    &lt;th&gt;A&lt;/th&gt;
    &lt;th&gt;B&lt;/th&gt;
     &lt;th&gt;C&lt;/th&gt;
   &lt;/tr&gt;
  &lt;/thead&gt;
 &lt;tbody&gt;
  &lt;tr&gt;
   &lt;td&gt;Germany&lt;/td&gt;
   &lt;td&gt;Yes&lt;/td&gt;
   &lt;td&gt;No&lt;/td&gt;
   &lt;td&gt;Yes&lt;/td&gt;
  &lt;/tr&gt;
  .........
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;


In building a string of HTML, I use an each(function) for each returned object and then at the end set the div #area with the content. I can't think of a better way of doing this, but would like to know if there is a way to simplify the code. My actual JSON Data is way bigger than the example below, so the code would be very long and use far more each function in the buildup of the string.

JSON:

[{"title":"A","nation":"Germany,Japan","city":"Hamburg,Toyko","Name":"John,Peter,Tom"},{"title":"B","nation":"Japan,Italy","city":"Toyko","Name":"Adam,Tom"},{"title":"C","nation":"Germany","city":"Berlin,Hamburg","Name":"Mary,Tom"}]


Code:

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

            item_html += "&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;&lt;/th&gt;";  

            $(data.query.results.json.json).each(function (index, item) {
                var title = item.title;
                item_html += '&lt;th&gt;'+title+'&lt;/th&gt;';
            });

            item_html += "&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Germany&lt;/td&gt;";

            $(data.query.results.json.json).each(function (index, item) {
                var nations = item.nation.replace(/ /g,'').split(",");
                console.log(nations);
                if ($.inArray('Germany', nations) &lt; 0)
                {
                  item_html += "&lt;td&gt;No&lt;/td&gt;";
                }else{
                  item_html += "&lt;td&gt;Yes&lt;/td&gt;";
                }
                 });    

             item_html += "&lt;/tr&gt;&lt;td&gt;Berlin&lt;/td&gt;";

             $(data.query.results.json.json).each(function (index, item) {
                var  citys = item.city.replace(/ /g,'').split(",");
                if ($.inArray('Berlin', citys) &lt; 0)
                {
                  item_html += "&lt;td&gt;No&lt;/td&gt;";
                }else
                {
                  item_html += "&lt;td&gt;Yes&lt;/td&gt;";
                }

             });

              item_html += "&lt;/tr&gt;&lt;td&gt;Peter&lt;/td&gt;";

             $(data.query.results.json.json).each(function (index, item) {
                var  names = item.Name.replace(/ /g,'').split(",");
                if ($.inArray('Peter', names) &lt; 0)
                {
                  item_html += "&lt;td&gt;No&lt;/td&gt;";
                }else
                {
                  item_html += "&lt;td&gt;Yes&lt;/td&gt;";
                }

             });

           item_html += "&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;";

           $('#tablearea').append(item_html);

        }
    });
you can merge 

 var item_html="";

 item_html += "&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;&lt;/th&gt;";  


to

var item_html="&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;&lt;/th&gt;";  


and i also cant see the id "tablearea" in your html.
UPDATE:
by your fiddle link that tablearea is the id of a div and are you sure that you want to use $('#tablearea').append(item_html); because it will append to div everytimes whenever your ajax method called.

so better that you use

$('#tablearea').html(item_html);