根据属性值过滤掉数组对象 - AngularJS

时间:2018-05-21 14:12:23

标签: angularjs

我的组件中有一系列潜在客户,但需要过滤掉所选的等于true的所有潜在客户并将其存储在变量中。

我如何使用AngularJS做到这一点?

这是我目前所拥有的,但它以未定义的方式回归:

var list = $filter("filter")($scope.leads, {
            selected: true
});

请注意,某些对象具有以下属性:"已选择":为真,有些对象没有。

请告诉我您需要的更多信息(如果有的话)。

由于

2 个答案:

答案 0 :(得分:1)

我举了一个如何按选择过滤的例子。看一看。

    var userLeaves = new Dictionary<int, double>();
    foreach( var lr in leavesresult)
    {
        foreach (var leave in lr.LeaveList) 
        {
            if (leave.Status == "1" && leave.LeaveType != "3") 
            {
                var leaveDay = leave.check_halfday ==1 ? 0.5 : 1;
                if (userLeaves.ContainsKey(leave.UserID))
                    userLeaves[leave.UserID] = userLeaves[leave.UserID] + leaveDay;
                else
                    userLeaves.Add(leave.UserID, leaveDay);
            }
        }
    }
var leads=[
{"name":"one","selected":true},
{"name":"two","selected":false},
{"name":"three"},
{"name":"four","selected":true}
];

var list = leads.filter(function(lead) {
           return lead.selected
});

console.log(list);

答案 1 :(得分:0)

无法重现问题。

&#13;
&#13;
angular.module("app",[])
.run(function($filter) {
    var $scope_leads=[
    {"name":"one","selected":true},
    {"name":"two","selected":false},
    {"name":"three"},
    {"name":"four","selected":true}
    ];

    var list = $filter("filter")($scope_leads, {
                selected: true
    });
    console.log(list);
})
&#13;
<script src="//unpkg.com/angular/angular.js"></script>
<body ng-app="app">
<body>
&#13;
&#13;
&#13;