简体   繁体   English

从对象数组中删除重复项

[英]Removing duplicates from an array of objects

I have an object which has json objects called 我有一个具有json对象的对象

mainobject = 主要对象=

 Array[2]
>0: object
 >Innerobject1 : Array[2]
  >0 : object
     Name : "Xavier"
     Dup  : "B"
  >1 : object
     Name : "Gh"
     Dup : "B"
>1: object
 >Innerobject2 : Array[2]
  >0 : object
     Name : "Cat"
     Dup :  "C"
  >1 : object
     Name : "Dog"
     Dup : "D"

I need to make the "dup" as "" which was already present in the first object if any .My expected output is: 我需要将第一个对象中已经存在的“ dup”设置为“”,如果有的话。我的预期输出是:

Array[2]
>0: object
 >Innerobject1 : Array[2]
  >0 : object
     Name : "Xavier"
     Dup  : "B"
  >1 : object
     Name : "Gh"
     Dup : ""
>1: object
 >Innerobject2 : Array[2]
  >0 : object
     Name : "Cat"
     Dup :  "C"
  >1 : object
     Name : "Dog"
     Dup : "D"

Edit: 编辑:

The object in json format: : json格式的对象:

"[{"Innerobject1":[{"Name" :"Xavier","Dup":"B"},{"Name" :"Gh","Dup":"B"}]},
{"Innerobject2":[{"Name" : "Cat","Dup":"C"},{"Name":"Dog", "Dup":"D"}]}]"

I'm not quite sure that I have interpreted your posted array of objects correct. 我不太确定我是否正确解释了您发布的对象数组。 But you can do something like this: 但是您可以执行以下操作:

Iterate over the array and store the key you want to be unique in an object. 遍历数组并将要唯一的键存储在对象中。 When encountered more then once set the new value to an empty string: 如果遇到更多情况,则将新值设置为空字符串:

var seen = {};
mainobject.forEach(function(obj) {
   if (seen[obj.Name]) {
       obj.Name = "";
   }
   seen[obj.Name] = true;
});

You might need multiply iterations, dependents on how many nested arrays you got: 您可能需要多次迭代,具体取决于您获得了多少个嵌套数组:

var seen = {};
mainobject.forEach(function(inner_arr) {
   inner_arr.forEach(function(obj) {
       if (seen[obj.Name]) {
           obj.Name = "";
       }
       seen[obj.Name] = true;
    });
});

The solution using Array.forEach and Object.keys functions: 使用Array.forEachObject.keys函数的解决方案:

var mainobject = JSON.parse('[{"Innerobject1":[{"Name" :"Xavier","Dup":"B"},{"Name" :"Gh","Dup":"B"}]},{"Innerobject2":[{"Name" : "Cat","Dup":"C"},{"Name":"Dog", "Dup":"D"}]}]');

mainobject.forEach(function(obj){
    Object.keys(obj).forEach(function(k){
        obj[k].forEach(function(o){
            if (this["Dup"]) {
                (this["Dup"].indexOf(o["Dup"]) !== -1)? o["Dup"] = "" : this["Dup"].push(o["Dup"]);
            } else {
                this["Dup"] = [o["Dup"]];
            }
        })
    });
}, {});

console.log(JSON.stringify(mainobject, 0, 4));

The console.log output: console.log输出:

[
    {
        "Innerobject1": [
            {
                "Name": "Xavier",
                "Dup": "B"
            },
            {
                "Name": "Gh",
                "Dup": ""
            }
        ]
    },
    {
        "Innerobject2": [
            {
                "Name": "Cat",
                "Dup": "C"
            },
            {
                "Name": "Dog",
                "Dup": "D"
            }
        ]
    }
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM