我有一个JSON对象数组,我正在尝试将列表过滤到仅存在特定字段值的对象。
我想要过滤的字段可以包含几乎所有内容,所以除了存在值之外,我无法真正寻找其他内容。
我需要在angularjs控制器中应用过滤器。
如果我匹配某个特定值(例如'bob'),则以下情况有效,但并非所有对象都具有“newName”的值。
$scope.CorrectedNames = $filter('filter')($scope.dataList, { newName: 'bob' }).length;
console.log('Total Names to Update: '+$scope.totalCorrectedAccountNumbers.length);
我试过
newName: '!'
这会返回零结果。
newName: '!""'
这将返回整个json列表
他们是我从搜索中得到的唯一两个想法。
字段值为空且不为NULL,否则我认为以下内容可行。
newName: '!=null'
答案 0 :(得分:1)
因此,您不希望使用newName属性值显示null或nullstring的对象。您应该使用纯js .filter方法,如下所示:
$scope.CorrectedNames = $scope.dataList.filter(function(a) {
if(a.newName) {
return a.newName.trim().length !== 0;
}
});
通过在$ scope.CorrectedNames数组中执行此操作,您将从dataList 获取所有对象,不包括带有null或nullstring的 newName或没有newName属性的对象。
<强> {{3}} 强>
另一种方法是您可以创建自定义过滤器&amp;在其实现中,您可以执行相同的.filter()方法用于过滤。