2016-01-21 118 views
-1

所以我能夠成功地從我的JSON對象中提取數據,我遇到的問題是數據被四次重複。有人能告訴我爲什麼會發生這種情況嗎?我從來沒有見過這個。這裏是JS:刪除重複的JSON對象

jQuery(function() { 
    jQuery.getJSON(NewConvData, function(data){ 
     var channelHTML = '' 
     jQuery.each(data, function(i){ 
      console.log(data); 
      for (var i in data.results) { 
      channelHTML += '<tr><td class="tg-yw4l">' + data.results[i].call_mine_status + '</td><td class="tg-yw4l">' + data.results[i].cdr_source + '</td><td class="tg-yw4l">' + data.results[i].tracking_number + '</td><td class="tg-yw4l">' + data.results[i].disposition + '</td><td class="tg-yw4l">' + data.results[i].duration + '</td><td class="tg-yw4l">' + data.results[i].external_id + '</td><td class="tg-yw4l">' + data.results[i].id + '</td><td class="tg-yw4l">' + data.results[i].is_outbound + '</td><td class="tg-yw4l">' + data.results[i].ouid + '</td><td class="tg-yw4l">' + data.results[i].repeat_call + '</td><td class="tg-yw4l">' + data.results[i].caller_id + '</td><td class="tg-yw4l">' + data.results[i].calldate + '</td><td class="tg-yw4l">' + data.results[i].ringto_number + '</td><td class="tg-yw4l"><a href="' + data.results[i].file_url + '">Recorded Call</a></td></tr>'; 
      } 
     }); 
     jQuery('#conv_table').append(channelHTML); 
    });    
});   

這裏是Fiddle link

+0

你要做'的jQuery( '#conv_table')。空()''之前的append()' –

+1

你有2個循環'each'和'for'都檢查https://jsfiddle.net/cmw3co4s/3/ –

回答

1

你有2個循環eachfor都檢查

var channelHTML = '' 
      for (var i in data.results) { 
      channelHTML += '<tr><td class="tg-yw4l">' + data.results[i].call_mine_status + '</td><td class="tg-yw4l">' + data.results[i].cdr_source + '</td><td class="tg-yw4l">' + data.results[i].tracking_number + '</td><td class="tg-yw4l">' + data.results[i].disposition + '</td><td class="tg-yw4l">' + data.results[i].duration + '</td><td class="tg-yw4l">' + data.results[i].external_id + '</td><td class="tg-yw4l">' + data.results[i].id + '</td><td class="tg-yw4l">' + data.results[i].is_outbound + '</td><td class="tg-yw4l">' + data.results[i].ouid + '</td><td class="tg-yw4l">' + data.results[i].repeat_call + '</td><td class="tg-yw4l">' + data.results[i].caller_id + '</td><td class="tg-yw4l">' + data.results[i].calldate + '</td><td class="tg-yw4l">' + data.results[i].ringto_number + '</td><td class="tg-yw4l"><a href="' + data.results[i].file_url + '">Recorded Call</a></td></tr>'; 
      } 
      jQuery('#conv_table').append(channelHTML); 
     }); 

Working Fiddle

1

不要使用兩個循環的同樣的東西,刪除

for (var i in data.results)此循環並通過jQuery.each(data.results, function(i)修改jQuery.each(data, function(i)

該守則將工作完美,它不會重複。

這裏是小提琴鏈接Fiddle link