How to find an object in a list by comparing on object property

后端 未结 3 1002
时光说笑
时光说笑 2021-01-27 22:13

How could I scan an array of objects in order to find an object by matching the object property:

$scope.items = [
  { id: 1, name: \'one\' }, 
  { id: 2, name: \         


        
相关标签:
3条回答
  • 2021-01-27 22:39

    You can use Angular's built-in filter functionality to perform the search:

    $scope.filteredItems = function() {
        return $filter($scope.items, id == filterID);
    }
    

    Here is a fiddle showing the filter in action: http://jsfiddle.net/wittersworld/xV8QT/

    0 讨论(0)
  • 2021-01-27 22:49

    I Used Underscore js

    $scope.item = _.where($scope.items, { id: 1 });
    
    0 讨论(0)
  • 2021-01-27 22:57

    You can also use filter method like this:

    $scope.items.filter(function (item) {
        return item.id === 1; }
    )
    
    0 讨论(0)
提交回复
热议问题