-1

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.

1 Answer 1

1

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;
}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.