Attempted to extract specific data from a JSON file using $routeParams:id. Typically, I would use the following method:
var eventId = $routeParams.eventId;
$http.get('json/events.json')
.success(function(data){
angular.forEach(data,function(d){
if(d.id == eventId)$scope.event = d;
});
});
Came across different code that accomplishes the same task:
var eventId = $routeParams.eventId;
$http.get('json/events.json')
.success(function(data){
$scope.template = $filter('filter') (data, function(d){
return d.id == eventId;
})[0];
});
Could someone explain the syntax used in the second example? Specifically, the square brackets notation after the function call? Also, which approach is considered better?
Appreciate any help!