Get The Distinct Object From The Array Of Objects In Typescript Angular 8
I have an array of object as shown below: [{ 'name': 'Okta Verify Push', 'provider': 'OKTA', 'type': 'push', 'status': 0, 'id': 'opfhgfgaidhyBw2H90h7' }, {
Solution 1:
In the case that you have a preference for taking the first occurrence, you can first map the data into an object based on provider
being the key
and itself as the value
. Once that is done, you can then extract the all of the values with Object#values
.
const data = [{
"name": "Okta Verify Push",
"provider": "OKTA",
"type": "push",
"status": 0,
"id": "opfhgfgaidhyBw2H90h7"
}, {
"name": "Okta Verify TOTP",
"provider": "OKTA",
"type": "token:software:totp",
"status": 0,
"id": "osthgek5jmWTckcka0h7"
}, {
"name": "Unknown",
"provider": "CUSTOM",
"type": "claims_provider",
"status": 1,
"id": "clpn4wdtqtH6geILD0h7"
}, {
"name": "Google Authenticator",
"provider": "GOOGLE",
"type": "token:software:totp",
"status": 1,
"id": null
}]
const values = Object.values(
data.reduce((a, b) => {
if (!a[b.provider]) a[b.provider] = b
return a
}, {})
)
console.log(values)
Post a Comment for "Get The Distinct Object From The Array Of Objects In Typescript Angular 8"