Skip to content Skip to sidebar Skip to footer

Remove Duplicates From An Array Which Contains Array

I have a JSON array like this [{'Email':'someone@some.com','Name':'ACO','Groups':['MOD_SW','MOD_PI','MOD_GE'],'Id':63,'Url':'aco'}, {'Email':'someone@some.com','Name':'Agpo','Group

Solution 1:

2 nested loops could do the job:

var data = [... your data ...];
var groups = [];
$.each(data, function(i, item) {
    $.each(item.Groups, function(j, group) {
        if ($.inArray(group, groups) == -1) {
            groups.push(group);
        }
    });    
});

// at this stage groups = ["MOD_SW", "MOD_PI", "MOD_GE"]

and if you wanted to directly append options to your dropdown:

var groups = [];
$.each(data, function(i, item) {
    $.each(item.Groups, function(j, group) {
        if ($.inArray(group, groups) == -1) {
            groups.push(group);
            $(selectId).append(
                $('<option/>', {
                    value: group,
                    text: group
                })
            );
        }
    });    
});

UPDATE:

And to make this more efficient you could define a static dtstinct method:

$.extend({
    distinct : function(arr) {
       var result = [];
       $.each(arr, function(index, value) {
           if ($.inArray(value, result) == -1) {
               result.push(value);
           }
       });
       return result;
    }
});

and then use the .map method:

var data = [... your data ...];
var groups = $.distinct($(data).map(function() {
    returnthis.Groups;    
}));
$.each(groups, function(index, group) {
    $(selectId).append(
        $('<option/>', {
            value: group,
            text: group
        })
    );
});

Solution 2:

First look at the last group: "Groups":["MOD_PI,MOD_GE"] Don't you need to close the quotes after MOD_PI and open them after the comma or this is a set of groups?

If this with the quotes is problem the your script can look something like this:

var obj = [{"Email":"someone@some.com","Name":"ACO", "Groups":["MOD_SW","MOD_PI","MOD_GE"],"Id":63,"Url":"aco"},
{"Email":"someone@some.com","Name":"Agpo", "Groups":["MOD_PI"],"Id":22,"Url":"agpo"},
{"Email":"someone@some.com","Name":"Akatherm", "Groups":["MOD_SW"],"Id":64,"Url":"akatherm"},
{"Email":"someone@some.com","Name":"Albrand", "Groups":["MOD_PI","MOD_GE"],"Id":23,"Url":"albrand"}]

var temp = {}, 
    result = [];

for (var i = 0; i < obj.length; i+=1) {
    for (var j = 0; j < obj[i]['Groups'].length; j+=1) {
        if (typeof temp[obj[i]['Groups'][j]] === 'undefined') {
            result.push(obj[i]['Groups'][j]);
            temp[obj[i]['Groups'][j]] = true;
        }
    }
}

If this is a set of groups:

for (var i = 0; i < obj.length; i+=1) {
    for (var j = 0; j < obj[i]['Groups'].length; j+=1) {
        currentGroups = obj[i]['Groups'][j].split(',');
        for (var k = 0; k < currentGroups.length; k += 1) {
            currentGroups[k] = currentGroups[k].replace(/ /g,"");
            if (typeof temp[currentGroups[k]] === 'undefined') {
                result.push(currentGroups[k]);
                temp[currentGroups[k]] = true;
            }       
        }
    }
}

I think that's the most efficient way because you're checking for duplicates with O(1) and you don't have to do extra work (for example sort any array).

Post a Comment for "Remove Duplicates From An Array Which Contains Array"