Home > other >  Getting unique objects based on nested object property
Getting unique objects based on nested object property

Time:07-28

I have an object array as follows. I need the unique objects based the _id field of the application object inside the group.

const myObjArray:any = [
    {
        "group": {
            "name": "SAM-admin",
            "active": true,
            "application": {
                "_id": "g4fac238hma10kjo3mv473vbs",
                "name": "Prism",
                "owners": "",
                "url": "",
                "description": "",
                "active": "",
                "created_by": "",
                "updated_by": ""
            },
            "created_by": "",
            "updated_by": "",
        },
        "user": "g4fac238hma10kjo3mv473vbs",
        "role": "g4fac238hma10kjo3mv473vbs",
        "isAdmin": true,
        "active": true,
        "created_by": "[email protected]",
        "updated_by": "",
    },
        {
        "group": {
            "name": "SAM-super-admin",
            "active": true,
            "application": {
                "_id": "g4fac238hma10kjo3mv473asc",
                "name": "Utopia",
                "owners": "",
                "url": "",
                "description": "",
                "active": "",
                "created_by": "",
                "updated_by": ""
            },
            "created_by": "",
            "updated_by": "",
        },
        "user": "g4fac238hma10kjo3mv473asc",
        "role": "g4fac238hma10kjo3mv473asc",
        "isAdmin": true,
        "active": true,
        "created_by": "[email protected]",
        "updated_by": "",
    }
    ,
        {
        "group": {
            "name": "SAM-guest",
            "active": true,
            "application": {
                "_id": "g4fac238hma10kjo3mv473asc",
                "name": "Utopia",
                "owners": "",
                "url": "",
                "description": "",
                "active": "",
                "created_by": "",
                "updated_by": ""
            },
            "created_by": "",
            "updated_by": "",
        },
        "user": "g4fac238hma10kjo3mv473asc",
        "role": "g4fac238hma10kjo3mv473asc",
        "isAdmin": true,
        "active": true,
        "created_by": "[email protected]",
        "updated_by": "",
    }
];

Here in myObjArray the second and third elements are having group object with same application _id

Hence i need to remove the third object.

How to do this at the easiest way in typescript.

CodePudding user response:

As always, Array.reduce is perfect to grouping array of objects by property. Just pass along as the acc parameter an object that will collect all the items as you iterate. Finally, getting the Object.values of the result since we don't need the grouping keys.

var data = [{ "group": { "name": "SAM-admin", "active": true, "application": { "_id": "g4fac238hma10kjo3mv473vbs", "name": "Prism", "owners": "", "url": "", "description": "", "active": "", "created_by": "", "updated_by": "" }, "created_by": "", "updated_by": "", }, "user": "g4fac238hma10kjo3mv473vbs", "role": "g4fac238hma10kjo3mv473vbs", "isAdmin": true, "active": true, "created_by": "[email protected]", "updated_by": "", }, { "group": { "name": "SAM-super-admin", "active": true, "application": { "_id": "g4fac238hma10kjo3mv473asc", "name": "Utopia", "owners": "", "url": "", "description": "", "active": "", "created_by": "", "updated_by": "" }, "created_by": "", "updated_by": "", }, "user": "g4fac238hma10kjo3mv473asc", "role": "g4fac238hma10kjo3mv473asc", "isAdmin": true, "active": true, "created_by": "[email protected]", "updated_by": "", }, { "group": { "name": "SAM-guest", "active": true, "application": { "_id": "g4fac238hma10kjo3mv473asc", "name": "Utopia", "owners": "", "url": "", "description": "", "active": "", "created_by": "", "updated_by": "" }, "created_by": "", "updated_by": "", }, "user": "g4fac238hma10kjo3mv473asc", "role": "g4fac238hma10kjo3mv473asc", "isAdmin": true, "active": true, "created_by": "[email protected]", "updated_by": "", } ];

var result = (Object.values(data.reduce(function(acc, item) {
  if (!acc[item.group.application._id]) {
    acc[item.group.application._id] = item;
  }
  return acc;
}, {})));

console.log(result);
.as-console-wrapper {
  max-height: 100% !important;
}

  • Related