简体   繁体   中英

Removing duplicates from an array of objects

I have an object which has json objects called

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:

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: :

"[{"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:

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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