2014-09-30 94 views
0

以下是我正在使用的服務的一種方法。它從項目中找到所有收入(與此問題相關: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技能......任何幫助?

+0

您是否嘗試過使用'orderBy'過濾器來執行排序,要麼在'ngRepeat'中,要麼在您的控制器中? – 2014-09-30 16:46:10

+0

我可以在上面粘貼的數組上使用orderBy,但是我無法構造它。這是我面臨的問題。 – enguerranws 2014-10-01 08:14:03

+0

你可以發佈你想要的當前數據結構和最終結果嗎? – 2014-10-01 15:50:34

回答

0

我最後問一個朋友,誰是一個很好的Web開發人員幫我一下。他讓我在這兩個步驟中做到這一點構造對象,然後,以我需要的方式格式化它

所以我這樣做:

self.trimestral = function(){ 
    var deferred = $q.defer(); 
    var global = []; 
    self.buildAndGetIncomes().then(function(result) { 
    var trimestral = {}; 

    var incomes = result; 

    // First loop, construct the object 
    angular.forEach(incomes, function(income, i){ 
     var year = income.year, 
      trim = income.trim, 
      newTrim = {}, 
      newTrimContent = {};   
      if(trimestral[year] === undefined){ 
      trimestral['year' , year] = {}; 

      } 
      if(trimestral[year][trim] === undefined) { 
      trimestral[year][trim] = [];    
      } 
      trimestral[year][trim].push(income);   

    }); 

    deferred.resolve(global); 

    // Second loop, format the data 
    for (prop in trimestral) {   
     newYear = {}; 
     newYear.name = prop; 
     newYear.content = [];   
     for (trim in trimestral[prop]){ 
     newTrim = {}; 
     newTrim.name = trim; 
     newTrim.content = [];   
     newTrim.content = trimestral[prop][trim];    
     newYear.content.push(newTrim); 
     } 
     global.push(newYear); 
    }   
    }); 
return deferred.promise;  
}; 

我猜這可能是清潔的,但它的工作原理和它的更簡單的比我的預期。

1

這當然可以使用幾個循環,但代碼將很難遵循。如果您可以帶上另一個庫,請考慮underscore.js。有了它,你可以做這樣的事情......

var trimestral = _(incomes).chain() 
    .groupBy('year') 
    .map(function (items, year) { 
     return { 
      year: year, 
      trim: _(items).chain() 
        .groupBy('trim') 
        .map(function(content, trimester) { 
         return { 
          trim: trimester, 
          content: content 
         }; 
        }) 
        .sortBy(function(i) { 
         var trimesters = { 
          first: 1, 
          second: 2, 
          third: 3 
         }; 
         return trimesters[i.trim]; 
        }).value() 
     }; 
    }) 
    .sortBy('year') 
    .value(); 

Live Demo

+0

我想過了,我已經知道我可以使用jQuery $ .map()以更簡單的方式實現這一點。但我不想用另一個js庫:)謝謝你! – enguerranws 2014-10-04 11:27:52

相關問題