Kunal Vashist
Kunal Vashist

Reputation: 2471

Searching tag in object Angularjs

I have an tag array list which user enters

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


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

            },
            {
                "name": "Executive",
                "FirstName": "Jackey",
                "LastName": "Gordon",
                "Title": null,
                "email": "[email protected]",
                "place" : "INDIA"

            },
            {
                "name": "Executive",
                "FirstName": "Jackey",
                "LastName": "Gordon",
                "Title": null,
                "email": "[email protected]",
                "place" : "AUSTRALIA"

            },
            {
                "name": "Executive",
                "FirstName": "Jackey",
                "LastName": "Gordon",
                "Title": null,
                "email": "[email protected]",
                "place" : "SOUTHAFRICA"

            }      ]

I need to search $scope.tags i.e INDIA,USA,CHINA,JAPAN in $scope.object and return new array.

So new array object will be like

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

                },
                {
                    "name": "Executive",
                    "FirstName": "Jackey",
                    "LastName": "Gordon",
                    "Title": null,
                    "email": "[email protected]",
                    "place" : "INDIA"

                }]

Upvotes: 0

Views: 35

Answers (2)

Kalhan.Toress
Kalhan.Toress

Reputation: 21901

you can try something like this

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

here is the working plunker

Upvotes: 1

RandomUser
RandomUser

Reputation: 1853

Try this, I didn't test.

$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]);
    }
  }
}

Upvotes: 0

Related Questions