使用loadash angularjs过滤记录

时间:2018-02-13 06:30:37

标签: angularjs

var arr = Array [ Object, Object ]

into arr {
  "name" : "xyz",
  "age" : "22",
  "sports" : Array[
{
   id:1,
   name : "xyz",
},
{
   id:2,
   name : "yyy",
},
]
}

var obj = Object { name: "JXZ", id :1};

var response = _.filter(arr, function(user) {
                        return user.sports.id === obj.id;
                      });

从arr的sports.id返回数组对象中过滤与obj的id匹配的记录。 我想放循环,但我想这不是正确的解决方案,如果请

1 个答案:

答案 0 :(得分:0)

请尝试以下代码。

<script>
    var app = angular.module('app',[]);
    app.controller('ctrl',function($scope){
        $scope.arr = Array [ Object, Object ];

        $scope.arr = {
            "name" : "xyz",
            "age" : "22",
            "sports" : [
              {
                  id:1,
                  name : "xyz",
              },
              {
                  id:2,
                  name : "yyy",
              }
            ]
        }

        var obj =  { name: "JXZ", id :1};

        var filteredSports = _.filter($scope.arr.sports, function (sport) {
            return sport.id == obj.id;
        })

        $scope.arr.sports = filteredSports;
        console.log($scope.arr);
    })        
</script>