【问题标题】:trying to filter array of objects with array in itself试图用数组本身过滤对象数组
【发布时间】:2021-05-09 07:19:56
【问题描述】:

我像这样过滤一组对象:

[
{
  "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);

我正在尝试按具有以下属性的属性进行过滤: 具有 custom:organization 和值 zxc 的属性 我该怎么做??

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

我尝试了几种方法,但我在输出中得到了空数组 谢谢

【问题讨论】:

    标签: javascript arrays typescript filter


    【解决方案1】:

    你可以这样做:

    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

    【讨论】:

    • 它似乎是正确的,但它正在打印每条记录,无论值是 zxc 还是任何其他
    • 我的输出没有爆炸属性对象{ Username: 'ee558aa7-648b-403d-8464-b4da1081a8b9', Attributes: [ [Object], [Object], [Object], [Object] ], UserCreateDate: '2021-02-04T17:59:38.733Z', UserLastModifiedDate: '2021-02-04T17:59:38.733Z', Enabled: true, UserStatus: 'UNCONFIRMED' }
    • 您只需要属性对象吗?这将返回具有至少一个与过滤器匹配的属性的整个用户。此外,[Object] 是由于最大深度而隐藏对象的节点检查器。如果是 Node.js,则运行 console.log(require('util').inspect(output, { colors: true, depth: Infinity }))
    【解决方案2】:

    要过滤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"
      }
    ]
    

    【讨论】:

    • 它缺少属性数组中的其他对象。
    【解决方案3】:

    你可以试试这个

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      • 2021-08-27
      • 1970-01-01
      • 2018-10-03
      • 1970-01-01
      相关资源
      最近更新 更多