簡體   English   中英

試圖用數組本身過濾對象數組

[英]trying to filter array of objects with array in itself

我像這樣過濾 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"
},

以下是過濾罰款

let filterarry;
filterarry = jsonObject.filter(Attributes => {
  return Attributes.Enabled == true && Attributes.UserStatus == 'UNCONFIRMED';
});
console.log(filterarry);

我正在嘗試按具有以下屬性的屬性進行過濾:具有自定義:組織和值 zxc 的屬性我該怎么做?

"Attributes":[
 {
    "Name":"custom:organization",
    "Value":"zxc"
 },

我嘗試了幾種方法,但我在 output 中得到了空數組謝謝

你可以這樣做:

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

另請參閱: https://stackoverflow.com/a/8217584/14133230

要過濾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;
});

結果

[
  {
    "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"
  }
]

你可以試試這個

 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM