从包含数组的数组中删除重复项 [英] Remove duplicates from an array which contains array
问题描述
我有一个像这样的JSON数组
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"}]
我想用不同的Groups
创建一个新的数组(用于select标签).
I want to create a new array (for select tag) with distinct Groups
.
此Groups
是一个数组.
我希望该选择框具有以下值:
I want that selectbox to have the following values:
MOD_SW
MOD_PI
MOD_GE
我的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>");
});
}
我也尝试过
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]);
}
}
但是错误显示为:Groups.length
为空或不是对象
But error appears as: Groups.length
is null or not an object
此代码将select
标记附加到组值上,但是由于Groups
是一个数组,因此它显示为重复项.
This code appends to select
tag the Group values but it appears as duplicates because Groups
is an array.
我必须创建一个新的for
语句来浏览Groups
数组?
I have to create a new for
statement to browse the Groups
array ?
或者还有另一种方法可以避免嵌套的for
语句?
Or there is another alternative to avoid nested for
statements ?
谢谢
推荐答案
2个嵌套循环可以完成此工作:
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:
要使其更有效,您可以定义一个静态dtstinct
方法:
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;
}
});
,然后使用.map
方法:
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
})
);
});
这篇关于从包含数组的数组中删除重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!