2014-11-25 70 views
0

我正在運行ajax調用以從JSON結構檢索值。對於每次迭代,我將值添加到.push的js數組中。有多個相同值的實例(例如一個人的名字)。我想要的是統計相同名稱的數量,然後將其刪除。計數js數組中重複實例的數量

主要目標是將它們繪製在圖表中,然後顯示一個人的姓名以及他/她在數組中表示的時間數。

我的代碼:

var arr = []; 

$.each(data.d.results, function (a, data) { 
     $.ajax({ 
      url: "http://helpdesk.monjasa.com/IT/_vti_bin/ListData.svc/ITHelpdeskRequests(" + data.RequesterId + ")/CreatedBy", 
      headers: { 
       'accept': 'application/json;odata=verbose', 
       'content-type': 'application/json;odata=verbose' 
      }, 
      success: function (data2) { 
       $.ajax({ 
        url: "http://helpdesk.monjasa.com/IT/_vti_bin/ListData.svc/ITHelpdeskRequests(" + data.AssignedToId + ")/AssignedTo", 
        headers: { 
         'accept': 'application/json;odata=verbose', 
         'content-type': 'application/json;odata=verbose' 
        }, 
        success: function (data3) { 
         $(".inner").prepend('<p>' + data.Request + ' <br>Submitted by: ' + data2.d.Name + '<br>Assigned to: ' + data3.d.Name + ' | Due in: ' + data.DueInDays + ' day(s)</p>'); 
         arr.push(data3.d.Name); 
         console.log(arr); 
        } 
       }); 
      } 
     }); 

就如何實現這一目標的任何想法?

+0

的[jQuery的選擇唯一的項目]可能重複(HTTP ://stackoverflow.com/questions/1698499/jquery-select-unique-item) – Saksham 2014-11-25 09:59:20

回答

0

計算計數,而只有推到數組和做如果已經存在,則不推動。

var arr = []; 
var counts = []; 
$.each(data.d.results, function(a, data) { 
    $.ajax({ 
     url: "http://helpdesk.monjasa.com/IT/_vti_bin/ListData.svc/ITHelpdeskRequests("+data.RequesterId+")/CreatedBy", 
     headers: { 'accept': 'application/json;odata=verbose', 'content-type': 'application/json;odata=verbose'}, 
     success: function(data2){ 

      $.ajax({ 
        url: "http://helpdesk.monjasa.com/IT/_vti_bin/ListData.svc/ITHelpdeskRequests("+data.AssignedToId+")/AssignedTo", 
        headers: { 'accept': 'application/json;odata=verbose', 'content-type': 'application/json;odata=verbose'}, 
        success: function(data3){ 

          $(".inner").prepend('<p>'+data.Request +' <br>Submitted by: '+data2.d.Name+'<br>Assigned to: '+data3.d.Name+' | Due in: '+data.DueInDays+' day(s)</p>');  

        var indexOfName = arr.indexOf(data3.d.Name); 
        if(indexOfName == -1) { 
          arr.push(data3.d.Name); 
          counts.push(1); 
          console.log(arr); 
        } 
        else { 
          counts[indexOfName]++; 
        } 

        } 
       }); 

    } 
}); 
+0

Thx做到了。任何關於如何以兩次數組合並它們的方式來連接兩個數組的建議。示例'array 1 = [1,2,3] array 2 = ['Thomas','Christian','Tim']''array_final = ['name':'Thomas','number':1]?' – TietjeDK 2014-11-25 10:22:14

0

檢查重複最簡單的方法是加載的對象,這樣的事情:

// Ode to joy: 
var a = [3, 3, 4, 5, 5, 4, 3, 2, 1, 1, 2, 3, 3, 2, 2]; 

// Then count them 
var count = {}; 
for (var i = 0; i < a.length; i++) { 
    v = a[i]; 
    if (count[i] === undefined) count[i] = 0; 
    count[i]++ 
} 

console.log(count); 
0

這應該做的魔力

function getCounts(arr) { 
     var i = arr.length, // var to loop over 
      result = {}; // obj to store results 
     while (i) result[arr[--i]] = (result[arr[i]] || 0) + 1; // count occurrences 
     return result; 
    } 

    function getCount(word, arr) { 
     return getCounts(arr)[word] || 0; 
    } 

    getCount('Ehsan', yourArray); 
0

設置一個附加對象來接收人員姓名及其出現次數。

var arr = [], persons = {}; 

然後你內心success -callback可能是:

success: function (data3) { 
    var name = data3.d.Name; 
    $(".inner").prepend('<p>' + data.Request + ' <br>Submitted by: ' + data2.d.Name + '<br>Assigned to: ' + data3.d.Name + ' | Due in: ' + data.DueInDays + ' day(s)</p>'); 
    if (name in persons) persons[name]++; 
    else arr.push(name), persons[name] = 1; 
} 

之後,您可以顯示人及其OCCURENCES:

for (var n in persons) console.log(n + ': ', persons[n]);