以下是我正在使用的服務的一種方法。它從項目中找到所有收入(與此問題相關:Angular service calling another service)並創建一個新的對象數組。AngularJS:檢查對象中是否存在鍵/值並構建對象數組
我希望這個數組的對象能夠在年份和三個月中對收入進行排序,所以我可以在視圖中輕鬆地循環查看(使用ng-repeat)。
我現在的數據(由Incomes.buildAndGetIncomes()
返回,你可以看到如下圖):
[
{
"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"
}
]
的數據結構,我想:
[
{
year: 2014,
trim: [
{
name : 'first',
content : [
// some content
]
},
{
name : 'second',
content : [
// some content
]
}
]
},
{
year: 2013,
trim: [
{
name : 'first',
content : [
// some content
]
}
]
}
]
這裏是我現在使用的方法:
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;
};
此代碼按假定的方式工作,它檢查我們是否在fi這個循環的第一個索引,並且推動該學期的年/三學期/內容。這個結構是可以的。
現在我的問題是我需要檢查一年是否已經存在,然後檢查該孕期是否存在於那一年的對象中,以構建一個像上面粘貼的對象數組。
我嘗試了很多方法來做到這一點,但它似乎太難以我的JS技能......任何幫助?
您是否嘗試過使用'orderBy'過濾器來執行排序,要麼在'ngRepeat'中,要麼在您的控制器中? – 2014-09-30 16:46:10
我可以在上面粘貼的數組上使用orderBy,但是我無法構造它。這是我面臨的問題。 – enguerranws 2014-10-01 08:14:03
你可以發佈你想要的當前數據結構和最終結果嗎? – 2014-10-01 15:50:34