In one of the controllers of my angular application i have a variable set as follows.
SomeService.get({}, function(data){
// this sets xyz as the list of the data retrieved
// from the resource within the controllers scope
$scope.xyz = data.objects;
});
Now $scope.xyz
looks something like
[
0: {id: 1, ...more data here ...},
1: {id: 2, ...more data here ...},
2: {id: 3, ...more data here ...},
3: {id: 4, ...more data here ...},
4: {id: 5, ...more data here ...},
5: {id: 6, ...more data here ...},
]
What i am trying to do is get an object within xyz using the id
property (not the list index). I am aware that I can iterate over the array as follows.
angular.forEach($scope.xyz, function(obj){ return obj.id == 1;});
but is there a way I can do it without looping over the list ?
In one of the controllers of my angular application i have a variable set as follows.
SomeService.get({}, function(data){
// this sets xyz as the list of the data retrieved
// from the resource within the controllers scope
$scope.xyz = data.objects;
});
Now $scope.xyz
looks something like
[
0: {id: 1, ...more data here ...},
1: {id: 2, ...more data here ...},
2: {id: 3, ...more data here ...},
3: {id: 4, ...more data here ...},
4: {id: 5, ...more data here ...},
5: {id: 6, ...more data here ...},
]
What i am trying to do is get an object within xyz using the id
property (not the list index). I am aware that I can iterate over the array as follows.
angular.forEach($scope.xyz, function(obj){ return obj.id == 1;});
but is there a way I can do it without looping over the list ?
Share Improve this question asked Mar 14, 2013 at 13:57 AmythAmyth 33k27 gold badges96 silver badges137 bronze badges 4- Check the answer to this question. The remendation is to use the jQuery grep function. – ckersch Commented Mar 14, 2013 at 14:02
- Are you trying to go without underscore/lo-dash or similar utility belts? – Stewie Commented Mar 14, 2013 at 14:24
- @Stewie well I am open to options, but it would be good if there is a way without depending on a lib for such a small use case. – Amyth Commented Mar 15, 2013 at 9:07
- Well, Angular does not try to offer such low-level utilities, so this question is all about JavaScript solution for loop-less mapping. For that matter I'd say @Bergi answer is the best you can expect. – Stewie Commented Mar 15, 2013 at 9:44
2 Answers
Reset to default 8Though this is already answered a year ago, since this is one of the top results in Google, I thought I can add the following suggestion which may be the easiest way to filter. After injecting $filter into your controller,
var result = $filter('filter')($scope.xyz, {id:"1"});
reference: https://docs.angularjs/api/ng/filter/filter
No, you can't really avoid looping (O(n)
) unless there are some preconditions met.
- If the array is sorted by id, you can use a binary search algorithm (
O(log n)
). - If the array index always corresponds to the id-1 (or some similar simple formula), you can directly access it in
O(1)
.
If those conditions are not fulfilled initially, but you need to access the items by id multiple times, it could be helpful to bring them in that form (sorting/building a hash map).