角JS - 过滤和
问题描述:
集团
我有一个数据集合我正在从我的数据库为JSON回来。我需要通过这些结果进行过滤,并且每个销售代表我找回的名称。诀窍是,每个销售代表是不止一次在我的结果,但在我的最终名单,我只希望被包含一次名。角JS - 过滤和
因此,像萨拉,本,本,约翰·麦克,路加,约翰
将返回萨拉,本,约翰·麦克,卢克
$http({
url: '/Clients/Orders/Json',
method: "GET",
params: {}
}).success(function (data) {
var JSON = data;
$scope.data = JSON;
});
我的数据在$范围发现。数据。
我找上建立一个过滤器,通过运行这个数据,然后如何访问我的看法页面上的过滤信息的想法。
答
过滤器 -
app.filter('distinct', function() {
return function(arrayInput) {
var distinctArray = [];
for (var i = 0; i < arrayInput.length; i++) {
if(distinctArray.indexOf(arrayInput[i]) < 0){
distinctArray.push(arrayInput[i]);
}
}
return distinctArray;
};
});
然后注入你在控制器或服务过滤distinctFilter
从数组中筛选出不同的名称$scope.data
app.controller('MyCtrl', function($scope, distinctFilter) {
...
// populate $scope.data with array of names
$scope.distinctNames = distinctFilter($scope.data);
});