In Angular, I need to search objects in an array

后端 未结 7 1244
时光说笑
时光说笑 2020-12-04 07:30

In Angular, I have in scope a object which returns lots of objects. Each has an ID (this is stored in a flat file so no DB, and I seem to not be able to user ng-resour

7条回答
  •  情话喂你
    2020-12-04 08:00

    Your solutions are correct but unnecessary complicated. You can use pure javascript filter function. This is your model:

         $scope.fishes = [{category:'freshwater', id:'1', name: 'trout', more:'false'},  {category:'freshwater', id:'2', name:'bass', more:'false'}];
    

    And this is your function:

         $scope.showdetails = function(fish_id){
             var found = $scope.fishes.filter({id : fish_id});
             return found;
         };
    

    You can also use expression:

         $scope.showdetails = function(fish_id){
             var found = $scope.fishes.filter(function(fish){ return fish.id === fish_id });
             return found;
         };
    

    More about this function: LINK

提交回复
热议问题