我坚持使用Javascript基础知识..在Angular中工作。如何从具有ID的数组中获取多个对象?
我有对象的数组:
$scope.persons = [
{
id: 1,
name:'jack'
},
{
id: 2,
name:'John'
},
{
id: 3,
name:'eric'
},
{
id: 2,
name:'John'
}
]
我想获得的是与用户id相同ID的所有对象。因此,如果对象ID与用户ID相匹配,则通过对象循环选择它。
$scope.getResult = function(userId){
$scope.userId = userId;
for(var i=0;i < $scope.persons.length; i++){
if($scope.persons[i].id === $scope.userId){
$scope.result = $scope.persons[i];
}
}
$scope.userLogs = $scope.result;
};
我在这里只得到与userId具有相同id的最后一个对象。
如何列出与userId具有相同ID的所有对象?
直播:http://jsfiddle.net/sb0fh60j/
日Thnx提前!