オブジェクトのプロパティを照合してオブジェクトを見つけるために、オブジェクトの配列をスキャンするにはどうすればよいですか。
$scope.items = [
{ id: 1, name: 'one' },
{ id: 2, name: 'two' },
{ id: 3, name: 'three' }
];
$scope.item = $scope.items.find({ id: 1 }); // pseudo-code
オブジェクトのプロパティを照合してオブジェクトを見つけるために、オブジェクトの配列をスキャンするにはどうすればよいですか。
$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/
アンダースコアjsを使用しました
$scope.item = _.where($scope.items, { id: 1 });
次のようなフィルタメソッドを使用することもできます。
$scope.items.filter(function (item) {
return item.id === 1; }
)