简体   繁体   English

通过AngularJS中包含子字符串的属性过滤对象数组

[英]Filter an array of objects by a property containing a substring in AngularJS

Let's say i have an array like: 假设我有一个像这样的数组:

array = [{
    title: "foo1",
    content: "bar1"
},{
    title: "foo2",
    content: "bar2"
},{
    title: "foo3",
    content: "bar3"
}];

Now i want to filter this array to have the objects that their title contains a character like '3'. 现在我想过滤这个数组,让它们的标题包含像'3'这样的字符。 So now my filtered array should be 所以现在我的过滤数组应该是

filteredArray = [{
    title:"foo3",
    content: "bar3"
}];

I've tried 我试过了

    filteredArray = $filter('filter')(array, {
        title: "foo3"
    });

But the problem with this is that title needs to be exactly "foo3". 但问题是标题需要完全“foo3”。 if i put "3" it won't filter that because it doesn't check if it contains it, it looks for an exact match. 如果我把“3”它不会过滤,因为它不检查它是否包含它,它寻找完全匹配。

Any ideas how to achieve this? 任何想法如何实现这一目标?

The filter filter (yeah, I know) does a contains filtering... 过滤器过滤器(是的,我知道)包含过滤...

I pasted your code (working) into PLNKR and filtered on 3 and got back the title: 'foo3' element 我将你的代码(工作)粘贴到PLNKR并在3过滤并返回标题:'foo3'元素

array = [{
  title: "foo1",
  content: "bar1"
},{
  title: "foo2",
  content: "bar2"
},{
  title: "foo3",
  content: "bar3"
}];

$scope.filteredData = $filter('filter')(array, {
  title: "3",
});

You would need to write your own filter. 您需要编写自己的过滤器。 Check this answer on how to achieve what you want. 检查这个答案如何实现你想要的。

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

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