Here's a method of a service I'm using. It finds all incomes from projects (related to this question : Angular service calling another service) and make a new array of objects.
I want that array of objects to sort incomes by year and trimester, so I could loop into it easily in the view (with ng-repeat).
My current data (returned by Incomes.buildAndGetIncomes()
, you can see below) :
[
{
"projectName":"Deuxième test",
"clientName":"Deuxième client",
"typeIncome":"Accompte",
"amount":1000,
"date":"2014-09-10",
"notes":"Chèque / LDD",
"trim":"third",
"year":"2014"
},
{
"projectName":"efzefze",
"clientName":"zfezefzef",
"typeIncome":"Accompte",
"amount":30,
"date":"2014-09-10",
"notes":"fzefzef",
"trim":"third",
"year":"2014"
},
{
"projectName":"Nouveau test",
"clientName":"Nouveau client",
"typeIncome":"Accompte",
"amount":16,
"date":"2014-09-19",
"notes":"CC",
"trim":"third",
"year":"2014"
},
{
"projectName":"Nouveau projet",
"clientName":"Nouveau client",
"typeIncome":"Accompte",
"amount":1200,
"date":"2014-05-17",
"notes":"Chèque cc",
"trim":"second",
"year":"2014"
},
{
"projectName":"Projet test",
"clientName":"Client test",
"typeIncome":"Accompte",
"amount":1500,
"date":"2014-01-15",
"notes":"Chèque cc",
"trim":"first",
"year":"2014"
},
{
"projectName":"Deuxième test",
"clientName":"Deuxième client",
"typeIncome":"Reliquat",
"amount":4500,
"date":"2014-09-27",
"notes":"Virement",
"trim":"third",
"year":"2014"
},
{
"projectName":"efzefze",
"clientName":"zfezefzef",
"typeIncome":"Reliquat",
"amount":8,
"date":"2014-09-05",
"notes":"zefzefzef",
"trim":"third",
"year":"2014"
},
{
"projectName":"Nouveau test",
"clientName":"Nouveau client",
"typeIncome":"Reliquat",
"amount":7,
"date":"2014-09-27",
"notes":"LDD",
"trim":"third",
"year":"2014"
}
]
The data structure I want :
[
{
year: 2014,
trim: [
{
name : 'first',
content : [
// some content
]
},
{
name : 'second',
content : [
// some content
]
}
]
},
{
year: 2013,
trim: [
{
name : 'first',
content : [
// some content
]
}
]
}
]
And here's the method I'm using right now :
self.trimestral = function(){
var deferred = $q.defer();
self.buildAndGetIncomes().then(function(result) {
var trimestral = [];
var incomes = result;
angular.forEach(incomes, function(income, i){
var year = income.year,
trim = income.trim,
newTrim = {},
newTrimContent = {};
if( i === 0 ){
newTrim.year = year;
newTrim.trim = [];
newTrimContent ={};
newTrimContent.name = trim;
newTrimContent.content = [];
newTrimContent.content.push(income);
trimestral.push(newTrim);
console.log(trimestral);
trimestral[0].trim.push(newTrimContent);
}
else {
var maxLength = incomes.length;
for (var h = 0; h<maxLength; h++){
if(trimestral[h].year === year){
for (var j = 0; j<trimestral[h].trim.length; j++){
console.log(h,j,trimestral[h].trim[j].name === trim);
if(trimestral[h].trim[j].name === trim){ // trimester already exists
trimestral[h].trim[j].content.push(income);
}
else {// trimester doesn't exist, create it
var createTrim = {};
createTrim.name = trim;
createTrim.content = [];
createTrim.content.push(income);
trimestral[h].trim.push(newTrimContent);
}
}
}
else {
newTrim.year = year;
newTrim.trim = [];
newTrimContent ={};
newTrimContent.name = trim;
newTrimContent.content = [];
newTrimContent.content.push(income);
trimestral.push(newTrim);
console.log(trimestral);
trimestral[0].trim.push(newTrimContent);
}
}
}
});
deferred.resolve(trimestral);
});
return deferred.promise;
};
This code works as supposed, it checks if we're on the first index of the loop, and it push the year / trimester / content of that trimester. That structure is ok.
Now my problem is I need to check if the year already exists, and then, to check if the trimester exists in that year object, to construct an array of objects like I pasted above.
I tried many ways to do that, but it seems too difficult for my JS skills... Any help ?
orderBy
filter to perform the sorting, either in thengRepeat
s or inside your controller? – Anthony Chu Sep 30 '14 at 16:46