簡體   English   中英

如何通過比較對象屬性在列表中找到對象

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

如何通過匹配對象屬性來掃描對象數組以找到對象:

$scope.items = [
  { id: 1, name: 'one' }, 
  { id: 2, name: 'two' }, 
  { id: 3, name: 'three' }
];

$scope.item = $scope.items.find({ id: 1 }); // pseudo-code

您可以使用Angular的內置過濾器功能執行搜索:

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

這是一個顯示正在使用的過濾器的小提琴: http : //jsfiddle.net/wittersworld/xV8QT/

您也可以使用以下過濾方法:

$scope.items.filter(function (item) {
    return item.id === 1; }
)

我用下划線js

$scope.item = _.where($scope.items, { id: 1 });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM