【发布时间】:2016-10-03 10:41:34
【问题描述】:
我有数组对象的结果,需要用response_id 申请过滤器。此键存在于响应的子级中。如果我使用 23764、23765 进行搜索,我想要所有具有至少 2 个响应、id 为 23764 和 23765 的 Question 对象,这些是我想要在响应数组中唯一的两个响应对象。
如果我只使用 23764 进行搜索,我想要所有具有该 id 的响应对象的 Question 对象,并且只需要数组中的该响应对象。
我当前的实现尝试做这样的事情:
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope,$filter) {
$scope.data = [
{
"id": "1341",
"question": "What about this chaneel?",
"response": [
{
"response_id": "23764",
"comment": "Every thing "
},
{
"response_id": "23765",
"comment": "No"
},
{
"response_id": "23766",
"comment": ".."
}
]
},
{
"id": "1342",
"question": "What dislike about this chaneel?",
"response": [
{
"response_id": "23764",
"comment": "Nothing"
},
{
"response_id": "23765",
"comment": "No"
},
{
"response_id": "23766",
"comment": ".."
}
]
},
{
"id": "1343",
"question": "What suggestion(s) this chaneel?",
"response": [
{
"response_id": "23764",
"comment": "Nothing "
},
{
"response_id": "23765",
"comment": "No"
},
{
"response_id": "23766",
"comment": ".."
}
]
}
];
var res = ($filter('filter')($scope.data, {response:{response_id:'23764,23765'}}, true));
console.log(res);
});
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
期望的结果
[
{
"id": "1341",
"question": "What about this chaneel?",
"response": [
{
"response_id": "23764",
"comment": "Every thing "
},
{
"response_id": "23765",
"comment": "No"
}
]
},
{
"id": "1342",
"question": "What dislike about this chaneel?",
"response": [
{
"response_id": "23764",
"comment": "Nothing"
},
{
"response_id": "23765",
"comment": "No"
}
]
},
{
"id": "1343",
"question": "What suggestion(s) this chaneel?",
"response": [
{
"response_id": "23764",
"comment": "Nothing "
},
{
"response_id": "23765",
"comment": "No"
}
]
}
]
【问题讨论】:
-
您能描述一下您的目标吗?您使用
23764,23765作为过滤器值。例如,您想要or或and逻辑吗? -
@tasseKATT 需要
and -
@tasseKATT 我有逗号分隔值,该值已从视图中传递,需要在过滤器中应用
and而不是or -
我假设您不希望每个对象都有
response_id=23764和23765,因为没有。您希望每个大对象都有具有匹配 id 的响应对象,所以至少有两个响应对象?听起来像是一个非常具体的过滤器。 -
@tasseKATT 是的,你是对的,需要特定的过滤器来匹配对象中的 id 并相应地过滤。
标签: javascript angularjs json filter