0

I filtering group of object like this:

[
{
  "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 ??

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

I tried few methods but i am getting empty array in output Thanks

3 Answers3

1

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

0xLogN
  • 3,289
  • 1
  • 14
  • 35
  • it seems correct but it is printing every record whether the value is zxc or any other – Sohaib Mustafa Feb 04 '21 at 23:39
  • my output is not exploding attributes object `{ 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' } ` – Sohaib Mustafa Feb 05 '21 at 15:03
  • Are you expecting just the attributes object? This returns the whole user that has at least one attribute matching the filter. Also, the `[Object]` is the Node Inspector hiding the object due to max-depth. Run `console.log(require('util').inspect(output, { colors: true, depth: Infinity }))` if it's Node.js – 0xLogN Feb 05 '21 at 16:08
0

To filter jsonObject.Attributes you need to recreate and the variable

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"
  }
]
uingtea
  • 6,002
  • 2
  • 26
  • 40
0

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);
D. Seah
  • 4,472
  • 1
  • 12
  • 20