Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 尝试使用数组本身筛选对象数组_Javascript_Arrays_Typescript_Filter - Fatal编程技术网

Javascript 尝试使用数组本身筛选对象数组

Javascript 尝试使用数组本身筛选对象数组,javascript,arrays,typescript,filter,Javascript,Arrays,Typescript,Filter,我筛选一组对象,如下所示: [ { "Username":"00d9a7f4-0f0b-448b-91fc-fa5aef314d06", "Attributes":[ { "Name":"custom:organization", "Value":"zxc" }, { &q

我筛选一组对象,如下所示:

[
{
  "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"
 },
我尝试了几种方法,但在输出中得到了空数组 谢谢

您可以这样做:

let output=jsonObject.filter(条目=>{
//过滤内部阵列并检查
if(entry.Attributes.filter(attr=>attr.Name=='custom:organization'&&attr.Value=='zxc')。长度!==0){
返回true;
}
返回false;
});

另请参见:

要筛选
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对象=[{
“用户名”:“00d9a7f4-0f0b-448b-91fc-fa5aef314d06”,
“属性”:[{
“名称”:“自定义:组织”,
“值”:“zxc”
},
{
“名称”:“子”,
“值”:“00d9a7f4-0f0b-448b-91fc-fa5aef314d06”
},
{
“名称”:“已验证电子邮件”,
“值”:“假”
},
{
“名称”:“电子邮件”,
“值”:zigor@freeallapp.com"
}
],
“UserCreateDate”:“2021-02-04T17:59:28.212Z”,
“UserLastModifiedDate”:“2021-02-04T17:59:28.212Z”,
“启用”:正确,
“用户状态”:“未确认”
},
{
“用户名”:“07c16a30-b994-4267-9794-6fb20739abeb”,
“属性”:[{
“名称”:“自定义:组织”,
“价值”:“asd”
},
{
“名称”:“子”,
“值”:“07c16a30-b994-4267-9794-6fb20739abeb”
},
{
“名称”:“已验证电子邮件”,
“值”:“假”
},
{
“名称”:“电子邮件”,
“价值”:“2marwan.khatar。39@dankq.com"
}
],
“UserCreateDate”:“2021-02-04T17:56:13.787Z”,
“UserLastModifiedDate”:“2021-02-04T17:56:13.787Z”,
“启用”:正确,
“用户状态”:“未确认”
},
];
常量filterarry=oObjects.filter(属性=>{
返回属性已启用&&
attr.UserStatus==“未确认”&&
属性部分(p=>p.Name==“自定义:组织”&&p.Value==“zxc”);
});

控制台日志(filterarry)似乎正确,但它正在打印每个记录,无论值是zxc还是其他任何值。我的编辑应该解决这个问题。我的输出不是分解属性对象
{Username:'ee558aa7-648b-403d-8464-b4da1081a8b9',属性:[[object],[object],[object],[object]],UserCreateDate:'2021-02-04T17:59:38.733Z',UserLastModifiedDate:'2021-02-04T17:59:38.733Z',Enabled:true,UserStatus:'UNCONFIRMED'}
是否只需要属性对象?这将返回至少有一个属性与筛选器匹配的整个用户。另外,
[Object]
是由于最大深度而隐藏对象的节点检查器。运行
console.log(require('util').inspect(输出,{colors:true,depth:Infinity}))
如果它的Node.jsit缺少属性数组中的其他对象。