Remove duplicates from an array containing an 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","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"}] 

I want to create a new array (for the selected tag) with excellent Groups .

This Groups is an array.

I want this selectbox to have the following meanings:

 MOD_SW MOD_PI MOD_GE 

My JS:

 UpdateSelectMenu: function (selectId, data) { $(selectId).empty(); $(selectId).html("<option value='all' selected='selected'>All groups</option>"); var array_unique_values = []; for (var i = 0; i < data.Groups.length; i++) { for (var j = i+1; j < data.Groups.length; j++) { if (data.Groups[i] === data.Groups[j]) { j = ++i; } } array_unique_values.push(data.Groups[i]); } array_unique_values = array_unique_values.sort(); $.each(array_unique_values, function (k, v) { $(selectId).append("<option value='" + v + "'>" + v + "</option>"); }); } 

I also tried

 for (var i = 0; i < data.length; i++) { //browse whole data for (var j = 0; j < data[i].Groups.length; j++) { //browse Groups array for (var k = j + 1; j < data[i].Groups.length; k++) { if (data[i].Groups[j] === data[i].Groups[k]) { continue; } } array_unique_values.push(data[i].Groups[j]); } } 

But the error appears as: Groups.length is null or not an object

This code adds select tags to group values, but it appears as duplicates, because Groups is an array.

Do I need to create a new for statement to view the Groups array?

Or is there another alternative to avoid nested for statements?

thanks

0
source share
2 answers

2 nested loops could complete the task:

 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 want to directly add parameters to the drop-down list:

 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 it more efficient, you can 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() { return this.Groups; })); $.each(groups, function(index, group) { $(selectId).append( $('<option/>', { value: group, text: group }) ); }); 
+3
source

First look at the last group: "Groups":["MOD_PI,MOD_GE"] Do you need to close quotes after MOD_PI and open them after the decimal point or is it a set of groups?

If this with quotation marks is the problem, your script might 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 the most efficient way is because you check for duplicates using O (1), and you don't need to do extra work (e.g. sort any array).

+2
source

Source: https://habr.com/ru/post/1478970/


All Articles