0

オブジェクトのプロパティを照合してオブジェクトを見つけるために、オブジェクトの配列をスキャンするにはどうすればよいですか。

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

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

3 に答える 3

0

Angular の組み込みフィルター機能を使用して検索を実行できます。

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

フィルターの動作を示すフィドルは次のとおりです。http://jsfiddle.net/wittersworld/xV8QT/

于 2013-06-10T14:28:27.097 に答える
0

アンダースコアjsを使用しました

$scope.item = _.where($scope.items, { id: 1 });
于 2013-07-25T05:05:27.513 に答える
0

次のようなフィルタメソッドを使用することもできます。

$scope.items.filter(function (item) {
    return item.id === 1; }
)
于 2013-06-10T17:43:52.960 に答える