简体   繁体   English

Typescript数组对象过滤器

[英]Typescript array object filter

I'm trying to filter a array object in angular 5, this is my object 我正在尝试过滤角度为5的数组对象,这是我的对象

{
    "id_record": 2,
    "groupName": "PD",
    "count": 15,
    "userList": [{
            "name": "jeffhua",
            "nEmail": "jeffhua@abc.com"
        },
        {
            "name": "joey",
            "nEmail": "joey@abc.com"
        },
        {
            "name": "angelagosto",
            "nEmail": "angelagosto@abc.com"
        }
    ]
}

and here is my filter code 这是我的过滤器代码

return items.filter(it => {
  return it.userList.filter(dit => {
    return dit.name.toLowerCase().includes(filterText)
  });
});

What could be wrong? 有什么事吗

In your filter code, the second filter returns an array : 在您的过滤器代码中,第二个过滤器返回一个数组:

return it.userList.filter(dit => {
    return dit.name.toLowerCase().includes(filterText)
  });
// returns array

and the filter method awaits a boolean to be returned. 并且filter方法等待一个布尔值返回。 Try the following : 尝试以下方法:

return items.filter(it => {
  return it.userList.filter(dit => {
    return dit.name.toLowerCase().includes(filterText);
  }).length !== 0;
});

In my code snippet, I check if the inner filter result (an array) is empty or not. 在我的代码段中,我检查内部过滤器结果(数组)是否为空。 If not, you return true to the parent filter, which will keep the current object in the resulting array. 如果不是,则将true返回给父过滤器,该过滤器会将当前对象保留在结果数组中。


Breakdown (edit) 细目分类(编辑

The following returns a boolean, checking if the name in lowercase of one of your user contains filterText. 以下代码返回一个布尔值,检查用户之一的小写名称是否包含filterText。

return dit.name.toLowerCase().includes(filterText);

Then, this block returns an array containing all the values that returned true in the condition explained before : 然后,此块返回一个数组,其中包含在前面说明的条件下返回true的所有值:

return it.userList.filter(dit => {
    return dit.name.toLowerCase().includes(filterText);
})

But, for the parent part of the code snippet to filter the array, it needs a boolean as return value : 但是,要使代码段的父级部分能够过滤数组,它需要一个布尔值作为返回值:

return items.filter(it => {
  return // expect boolean
});

Then, the .length !== 0 at the end of : 然后, .length !== 0在末尾:

it.userList.filter(dit => {
    return dit.name.toLowerCase().includes(filterText);
})

makes it a boolean which should be reflecting the condition you're wanting. 使它成为一个布尔值,应反映您想要的条件。

In full (and commented) : 完整(并评论):

// filter returns an array filtered by the method
return items.filter(it => {
  // awaits a boolean return value, with filter returning an array.
  return it.userList.filter(dit => {
    // filter awaits a boolean as return value AND returns a boolean --> ok for filter
    return dit.name.toLowerCase().includes(filterText);
  }).length !== 0; // filter has returned an array, we cast it to boolean while checking if its length is greater than 0.
});

Use some instead of inner filter 使用some代替内部filter

  return items.filter(it => {
      return it.userList.some(dit => {
        return dit.name.toLowerCase().includes(filterText)
      });
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM