从 Typescript Angular 8 中的对象数组中获取不同的对象 [英] Get the distinct object from the array of objects in Typescript Angular 8
问题描述
我有一个对象数组,如下所示:
<代码>[{"name": "Okta 验证推送","provider": "OKTA",类型":推",状态":0,"id": "opfhgfgaidhyBw2H90h7"}, {"name": "Okta 验证 TOTP","provider": "OKTA",类型":令牌:软件:totp",状态":0,id":osthgek5jmWTckcka0h7"}, {"name": "未知","provider": "自定义","type": "claims_provider",状态":1,id":clpn4wdtqtH6geILD0h7"}, {"name": "谷歌身份验证器","provider": "GOOGLE",类型":令牌:软件:totp",状态":1,id":空}]
我想根据 **provider**
我试过了
[...new Set(item.filter(factor => factor.status == MultiFactorAuthenticationEnrolmentStatus.Enrolled).map(factor => factor.provider))];
>
这将返回数组字符串,例如 ["GOOGLE", "OKTA","CUSTOM"]
我的需求是获取对象的数组如
<代码>[{"name": "Okta 验证推送","provider": "OKTA",类型":推",状态":0,"id": "opfhgfgaidhyBw2H90h7"}, {"name": "未知","provider": "自定义","type": "claims_provider",状态":1,id":clpn4wdtqtH6geILD0h7"}, {"name": "谷歌身份验证器","provider": "GOOGLE",类型":令牌:软件:totp",状态":1,id":空}]
参考 - 如何获取JavaScript 中对象数组的不同值?
如果您有偏好取第一次出现的情况,您可以先将数据映射到基于 provider
的对象中作为 key
和它本身作为 value
.完成后,您可以使用 Object#values
提取所有值.
const data = [{"name": "Okta 验证推送","provider": "OKTA","type": "push",状态":0,"id": "opfhgfgaidhyBw2H90h7"}, {"name": "Okta 验证 TOTP","provider": "OKTA",类型":令牌:软件:totp",状态":0,id":osthgek5jmWTckcka0h7"}, {"name": "未知","provider": "自定义","type": "claims_provider",状态":1,id":clpn4wdtqtH6geILD0h7"}, {"name": "谷歌身份验证器","provider": "GOOGLE",类型":令牌:软件:totp",状态":1,id":空}]const 值 = Object.values(data.reduce((a, b) => {如果 (!a[b.provider]) a[b.provider] = b返回一个}, {}))console.log(values)
I have an array of object as shown below:
[{
"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
}]
I want to get the distinct object as array based on the **provider**
I tried
[...new Set(item.filter(factor => factor.status == MultiFactorAuthenticationEnrolmentStatus.Enrolled).map(factor => factor.provider))];
This returns string of array such as ["GOOGLE", "OKTA","CUSTOM"]
My requirement is to get the Array of Object such as
[{
"name": "Okta Verify Push",
"provider": "OKTA",
"type": "push",
"status": 0,
"id": "opfhgfgaidhyBw2H90h7"
}, {
"name": "Unknown",
"provider": "CUSTOM",
"type": "claims_provider",
"status": 1,
"id": "clpn4wdtqtH6geILD0h7"
}, {
"name": "Google Authenticator",
"provider": "GOOGLE",
"type": "token:software:totp",
"status": 1,
"id": null
}]
Reference - How to get distinct values from an array of objects in JavaScript?
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)
这篇关于从 Typescript Angular 8 中的对象数组中获取不同的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!