我正在尝试使用javascript删除数组数组中的重复条目,其中重复项将始终位于长度为1的数组中。例如
[ [23, 46, 43, 44]
[46]
[52, 51]
[53]
[44]
[55, 66] ]
在这种情况下,我想删除数组[46]和[44],但保留[53]。这些单个条目重复数组的数量是可变的。想法?
编辑:这就是我最终做的工作,其中stacks_array是数组数组。
function stacks_sanatizer(stacks_array){
var output_array = [],
used_cards =[];
used_cards[70] = undefined;
for (each_stak in stacks_array){
if((stacks_array[each_stak].length > 1) && (stacks_array[each_stak] !== "")){
for (i=0; i<stacks_array[each_stak].length;i++){
var card_number = stacks_array[each_stak][i].attrs.fill.substr(27).replace('.jpg)','');
used_cards[card_number - 1] = true;
}
}
}
for (each_stak in stacks_array){
if(stacks_array[each_stak] !== ""){
if(stacks_array[each_stak].length === 1){
card_number = stacks_array[each_stak][0].attrs.fill.substr(27).replace('.jpg)','');
if(used_cards[card_number - 1] !== true){
output_array.push(stacks_array[each_stak]);
used_cards[card_number - 1] = true;
}
}
else{
output_array.push(stacks_array[each_stak]);
}
}
}
//delete doubles
for(each_pile in output_array){
output_array[each_pile] = output_array[each_pile].filter(function(elem, pos) {
return output_array[each_pile].indexOf(elem) == pos;
})
}
return output_array;
}
答案 0 :(得分:1)
您可能希望使用哈希的某种组合来查找冲突,然后拼接您不想要的内容。
像这样:
function removeDuplicates(listOfLists) {
var hash = {};
for (var i = 0; i < listOfLists.length; i++) {
var array = listOfLists[i];
for (var j = 0; j < array.length; j++) {
var val = array[j];
var hashedVal = hash[val];
if (hashedVal === undefined) {
hash[val] = true;
}
else {
array.splice(j, 1);
if (array.length === 0) {
listOfLists.splice(i, 1);
}
}
}
}
}
答案 1 :(得分:1)
伪代码实现这一目标:
希望这会有所帮助:)