通过AngularJS中包含子字符串的属性过滤对象数组 [英] Filter an array of objects by a property containing a substring in AngularJS

查看:1502
本文介绍了通过AngularJS中包含子字符串的属性过滤对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个类似的数组:

Let's say i have an array like:

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

现在我想过滤这个数组,让它们的标题包含像'3'这样的角色。
所以现在我的过滤数组应该是

Now i want to filter this array to have the objects that their title contains a character like '3'. So now my filtered array should be

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

我试过

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

但问题是标题需要完全是foo3。如果我把3它不会过滤,因为它不会检查它是否包含它,它会查找完全匹配。

But the problem with this is that title needs to be exactly "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.

任何想法如何实现这个?

Any ideas how to achieve this?

推荐答案

过滤器过滤器(是的,我知道)包含过滤......

The filter filter (yeah, I know) does a contains filtering...

我将您的代码(工作)粘贴到 PLNKR 并过滤在 3 上取回标题:'foo3'元素

I pasted your code (working) into PLNKR and filtered on 3 and got back the title: 'foo3' element

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

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

这篇关于通过AngularJS中包含子字符串的属性过滤对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆