My question is how can I filter objects from an array by comparing the id
that is inside a "sub-object". Type:
animais [
{
id: 34,
nome: baleia,
categoria: {
id: 2,
nome: mamifero
},
{
id: 23,
nome: galinha,
categoria: {
id: 3,
nome: oviparo
}
}
]
Following this example, I would like to take only mammalian animals (% with% == 2).
I have a service that returns all the animals:
app.factory('Animais', function($http){
var animalList,
obj = {};
obj = {
getAnimais:function(callback) {
if(animalList) {
callback(animalList);
return false;
}
else {
$http({
method: 'GET',
url: 'http://example/api/animais'
}).success(function(data) {
obj.saveAnimais:(data);
callback(data);
})
}
},
saveAnimais:function(data) {
locaisList = data;
}
}
return obj;
});
And my id
is as follows:
app.controller('AnimaisCtrl', function($scope, $routeParams, $filter, Animais) {
var myFilter = $filter;
Animais.getAnimais(function(data) {
$scope.animais = myFilter('filter')(data.animais, {
id:$routeParams.id
});
});
});
In controller
previous, the user selects the category. Then it is redirected to the animals screen that will show the animals in the selected category.
Does anyone give me strength?