计算js数组中重复实例的数量

时间:2014-11-25 09:53:35

标签: javascript arrays ajax

我正在运行ajax调用以从JSON结构中检索值。对于每次迭代,我将值添加到具有.push的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);
                    }
                });
            }
        });

关于如何实现这一目标的任何想法?

4 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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);

答案 2 :(得分: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]++;
                    }

                    }  
                });

    }  
});

答案 3 :(得分:0)

设置其他对象以接收人名及其出现次数。

var arr = [], persons = {};

然后你的内心success - 回调可能是:

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;
}

之后,您可以显示这些人及其出现的情况:

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