[英]trying to filter array of objects with array in itself
I filtering group of object like this:我像这样过滤 object 组:
[
{
"Username":"00d9a7f4-0f0b-448b-91fc-fa5aef314d06",
"Attributes":[
{
"Name":"custom:organization",
"Value":"zxc"
},
{
"Name":"sub",
"Value":"00d9a7f4-0f0b-448b-91fc-fa5aef314d06"
},
{
"Name":"email_verified",
"Value":"false"
},
{
"Name":"email",
"Value":"zigor@freeallapp.com"
}
],
"UserCreateDate":"2021-02-04T17:59:28.212Z",
"UserLastModifiedDate":"2021-02-04T17:59:28.212Z",
"Enabled":true,
"UserStatus":"UNCONFIRMED"
},
{
"Username":"07c16a30-b994-4267-9794-6fb20739abeb",
"Attributes":[
{
"Name":"custom:organization",
"Value":"asd"
},
{
"Name":"sub",
"Value":"07c16a30-b994-4267-9794-6fb20739abeb"
},
{
"Name":"email_verified",
"Value":"false"
},
{
"Name":"email",
"Value":"2marwan.khatar.39@dankq.com"
}
],
"UserCreateDate":"2021-02-04T17:56:13.787Z",
"UserLastModifiedDate":"2021-02-04T17:56:13.787Z",
"Enabled":true,
"UserStatus":"UNCONFIRMED"
},
Following is filtering fine以下是过滤罚款
let filterarry;
filterarry = jsonObject.filter(Attributes => {
return Attributes.Enabled == true && Attributes.UserStatus == 'UNCONFIRMED';
});
console.log(filterarry);
I am trying to filter by Attributes having: Attributes that have custom:organization and with value zxc how can I do that??我正在尝试按具有以下属性的属性进行过滤:具有自定义:组织和值 zxc 的属性我该怎么做?
"Attributes":[
{
"Name":"custom:organization",
"Value":"zxc"
},
I tried few methods but i am getting empty array in output Thanks我尝试了几种方法,但我在 output 中得到了空数组谢谢
You can do this:你可以这样做:
let output = jsonObject.filter(entry => {
// filter the internal array and check
if (entry.Attributes.filter(attr => attr.Name === 'custom:organization' && attr.Value === 'zxc').length !== 0) {
return true;
}
return false;
});
See also: https://stackoverflow.com/a/8217584/14133230另请参阅: https://stackoverflow.com/a/8217584/14133230
To filter jsonObject.Attributes
you need to recreate and the variable要过滤jsonObject.Attributes
您需要重新创建和变量
let filterarry = jsonObject.filter(item => {
item.Attributes = item.Attributes.filter(attr => {
return attr.Name == "custom:organization" && attr.Value == "zxc"
})
// return true => to remove only value of jsonObject.Attributes but not the parent
return item.Attributes.length;
});
results结果
[
{
"Username": "00d9a7f4-0f0b-448b-91fc-fa5aef314d06",
"Attributes": [
{
"Name": "custom:organization",
"Value": "zxc"
}
],
"UserCreateDate": "2021-02-04T17:59:28.212Z",
"UserLastModifiedDate": "2021-02-04T17:59:28.212Z",
"Enabled": true,
"UserStatus": "UNCONFIRMED"
}
]
you can try this你可以试试这个
const oObjects = [{ "Username": "00d9a7f4-0f0b-448b-91fc-fa5aef314d06", "Attributes": [{ "Name": "custom:organization", "Value": "zxc" }, { "Name": "sub", "Value": "00d9a7f4-0f0b-448b-91fc-fa5aef314d06" }, { "Name": "email_verified", "Value": "false" }, { "Name": "email", "Value": "zigor@freeallapp.com" } ], "UserCreateDate": "2021-02-04T17:59:28.212Z", "UserLastModifiedDate": "2021-02-04T17:59:28.212Z", "Enabled": true, "UserStatus": "UNCONFIRMED" }, { "Username": "07c16a30-b994-4267-9794-6fb20739abeb", "Attributes": [{ "Name": "custom:organization", "Value": "asd" }, { "Name": "sub", "Value": "07c16a30-b994-4267-9794-6fb20739abeb" }, { "Name": "email_verified", "Value": "false" }, { "Name": "email", "Value": "2marwan.khatar.39@dankq.com" } ], "UserCreateDate": "2021-02-04T17:56:13.787Z", "UserLastModifiedDate": "2021-02-04T17:56:13.787Z", "Enabled": true, "UserStatus": "UNCONFIRMED" }, ]; const filterarry = oObjects.filter(attr => { return attr.Enabled && attr.UserStatus === 'UNCONFIRMED' && attr.Attributes.some(p => p.Name === "custom:organization" && p.Value === "zxc"); }); console.log(filterarry);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.