在对象Angularjs中搜索标签

时间:2014-12-03 04:21:14

标签: angularjs

我有一个用户输入的标签数组列表

$scope.tags = ["INDIA","USA","JAPAN","CHINA"];


$scope.object = [ {
                "name": "Executive",
                "FirstName": "Jackey",
                "LastName": "Gordon",
                "Title": null,
                "email": "admin@admin.com",
                "place" : "USA"
            },
            {
                "name": "Executive",
                "FirstName": "Jackey",
                "LastName": "Gordon",
                "Title": null,
                "email": "admin@admin.com",
                "place" : "INDIA"

            },
            {
                "name": "Executive",
                "FirstName": "Jackey",
                "LastName": "Gordon",
                "Title": null,
                "email": "admin@admin.com",
                "place" : "INDIA"

            },
            {
                "name": "Executive",
                "FirstName": "Jackey",
                "LastName": "Gordon",
                "Title": null,
                "email": "admin@admin.com",
                "place" : "AUSTRALIA"

            },
            {
                "name": "Executive",
                "FirstName": "Jackey",
                "LastName": "Gordon",
                "Title": null,
                "email": "admin@admin.com",
                "place" : "SOUTHAFRICA"

            }      ]

我需要在$ scope.object中搜索$ scope.tags,即INDIA,USA,CHINA,JAPAN并返回新数组。

所以新的数组对象就像

$scope.new = [ {
                    "name": "Executive",
                    "FirstName": "Jackey",
                    "LastName": "Gordon",
                    "Title": null,
                    "email": "admin@admin.com",
                    "place" : "USA"
                },
                {
                    "name": "Executive",
                    "FirstName": "Jackey",
                    "LastName": "Gordon",
                    "Title": null,
                    "email": "admin@admin.com",
                    "place" : "INDIA"

                },
                {
                    "name": "Executive",
                    "FirstName": "Jackey",
                    "LastName": "Gordon",
                    "Title": null,
                    "email": "admin@admin.com",
                    "place" : "INDIA"

                }]

2 个答案:

答案 0 :(得分:1)

你可以尝试这样的事情

$scope.newArr = [];
 angular.forEach($scope.object , function(val,key) {
     var exists = ($scope.tags).indexOf(val.place);
     if(exists >= 0) {
         $scope.newArr.push(val);
     }
});

这是工作plunker

答案 1 :(得分:0)

试试这个,我没有测试过。

$scope.newObj;
for (var i = 0; i < $scope.tags.length; i++) {
  for (var j = 0; j < $scope.object.length; j++) {
    if ($scope.tags[i].toLowerCase() == $scope.object[j].place.toLowerCase()) {
          $scope.newObj.push($scope.object[j]);
    }
  }
}