2017-01-23 27 views
3

我試圖重新集合一個複雜的對象數組。使用多個複合對象對數組項目進行組合

這裏是我的數組:

[ 
    { scenario: "Treasury", diagnostic: "good results", action: "Manage Financial Recovery"}, 
    { scenario: "Treasury", diagnostic: "good results", action: "Analyze the impact of your investments"}, 
    { scenario: "Treasury", diagnostic: "Significant decline", action: "Ensure an adequate"}, 
    { scenario: "Treasury", diagnostic: "Significant decline", action: "Pilot your cash"}, 
    { scenario: "Turnover", diagnostic: "Improve trade efficiency of your business", action: "Valorize your labels"}, 
    { scenario: "Turnover", diagnostic: "Improve trade efficiency of your business", action: "Analyze the opportunity"} 
    { scenario: "Turnover", diagnostic: "Improve trade efficiency of your firm", action: "Contacter un prestataire"} 
]; 

我想統一上面這一個數組:

[ 
    { 
    scenario: "Treasury", 
    diagnostics: [ 
     { 
      diagnostic : "good results", 
      actions: [ 
       "Manage Financial Recovery", 
       "Analyze the impact of your investments" 
      ] 
     } 
     { 
      diagnostic : "Significant decline", 
      actions: [ 
       "Ensure an adequate", 
       "Pilot your cash" 
      ] 
     } 
    ] 
    }, 
    { 
    scenario: "Turnover", 
    diagnostics: [ 
     { 
      diagnostic : "Improve trade efficiency of your business", 
      actions: [ 
       "Valorize your labels", 
       "Analyze the opportunity" 
      ] 
     } 
     { 
      diagnostic : "Improve trade efficiency of your firm", 
      actions: [ 
       "Contacter un prestataire" 
      ] 
     } 
    ] 
    } 
]; 

所以我試圖用JSBin統一我的數組,但我並沒有得到預期的結果,那麼獲得一個沒有重複項的數組的最有效方法是什麼?

+0

的[什麼是最有效的方法來GROUPBY對象的一個​​JavaScript陣列上?](HTTP可能重複:// stackoverflow.com/questions/14446511/what-is-the-most-efficient-method-to-groupby-on-a-javascript-array-of-objects) –

+0

可能,但我的數組和我的預期結果是更復雜的和回答並沒有幫助我解決用戶想要統一的其他問題中的問題Ÿ兩個鍵在我的陣列中我有很多重複的鍵要統一 –

+0

請嘗試http://stackoverflow.com/q/36196298/215552。這個問題和問題已經在SO上反覆提出。 –

回答

1

您可以使用迭代方法,併爲分組項key使用助手對象。

function getGrouped(array, keys, groupCB, children) { 
 
    var result = [], 
 
     hash = { _: result }; 
 

 
    groupCB = groupCB || function (o) { return o; }; 
 
    children = children || []; 
 
    array.forEach(function (a) { 
 
     keys.reduce(function (r, k, i) { 
 
      var o = {}; 
 
      if (!r[a[k]]) { 
 
       r[a[k]] = { _: [] }; 
 
       o[k] = a[k]; 
 
       o[children[i] || 'children'] = r[a[k]]._; 
 
       r._.push(o); 
 
      } 
 
      return r[a[k]]; 
 
     }, hash)._.push(groupCB(a)); 
 
    }); 
 
    return result; 
 
} 
 

 
var data = [{ scenario: "Treasury", diagnostic: "good results", action: "Manage Financial Recovery" }, { scenario: "Treasury", diagnostic: "good results", action: "Analyze the impact of your investments" }, { scenario: "Treasury", diagnostic: "Significant decline", action: "Ensure an adequate" }, { scenario: "Treasury", diagnostic: "Significant decline", action: "Pilot your cash" }, { scenario: "Turnover", diagnostic: "Improve trade efficiency of your business", action: "Valorize your labels" }, { scenario: "Turnover", diagnostic: "Improve trade efficiency of your business", action: "Analyze the opportunity" }, { scenario: "Turnover", diagnostic: "Improve trade efficiency of your firm", action: "Contacter un prestataire" }], 
 
    groupCB = function (o) { return o.action }, 
 
    keys = ['scenario', 'diagnostic'], 
 
    children = ['diagnostics', 'actions'], 
 
    result = getGrouped(data, keys, groupCB, children); 
 

 
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

+1

偉大的實現!但是如果代碼清晰可讀,這將會非常有用。 –

+0

好主意,你使用了一系列鍵的事實!這是我試圖得到的結果! –

+0

以上的優點,它可以用於更多的按鍵/小孩,如果你需要它,而不需要改變主算法。 –

0

你可以做如下:;

var data = [ { scenario: "Treasury", diagnostic: "good results", action: "Manage Financial Recovery"}, 
 
      { scenario: "Treasury", diagnostic: "good results", action: "Analyze the impact of your investments"}, 
 
      { scenario: "Treasury", diagnostic: "Significant decline", action: "Ensure an adequate"}, 
 
      { scenario: "Treasury", diagnostic: "Significant decline", action: "Pilot your cash"}, 
 
      { scenario: "Turnover", diagnostic: "Improve trade efficiency of your business", action: "Valorize your labels"}, 
 
      { scenario: "Turnover", diagnostic: "Improve trade efficiency of your business", action: "Analyze the opportunity"}, 
 
      { scenario: "Turnover", diagnostic: "Improve trade efficiency of your firm", action: "Contacter un prestataire"} 
 
      ], 
 
    hash = data.reduce(function(p,c){ 
 
         var fd = null; 
 
         p[c.scenario] = p[c.scenario] ? (fd = p[c.scenario].diagnostics.find(d => d.diagnostic === c.diagnostic), 
 
                  fd ? (fd.action.push(c.action),p[c.scenario]) 
 
                  : (p[c.scenario].diagnostics.push({diagnostic: c.diagnostic, action: [c.action]}),p[c.scenario])) 
 
                 : {scenario: c.scenario, diagnostics: [{diagnostic: c.diagnostic, action: [c.action]}]}; 
 
         return p; 
 
         },{}); 
 
    result = Object.keys(hash).map(k => hash[k]); 
 
console.log(JSON.stringify(result,null,4));

-1

人們也可以考慮實施只是使用查找爲局部注射的鞋底集電極函數引用,因此,單個reduce迭代/週期內施加的,它已經聚集體/創建所需要的數據結構體。

如Nina的方法所證明的那樣,實際上不需要將reduce循環嵌套到forEach迭代中,這也證明了後者的thisArgs的用法。 (不僅僅因爲這很難讀)。

熱度的方法使用reduce,但每個迭代步驟中會有一個find是一個迭代過......最後的結果取決於噴氣另外兩個迭代,keysmap

人類可讀代碼示例...

var diagnosticData = [ 
 
    { scenario: "Treasury", diagnostic: "good results", action: "Manage Financial Recovery"}, 
 
    { scenario: "Treasury", diagnostic: "good results", action: "Analyze the impact of your investments"}, 
 
    { scenario: "Treasury", diagnostic: "Significant decline", action: "Ensure an adequate"}, 
 
    { scenario: "Treasury", diagnostic: "Significant decline", action: "Pilot your cash"}, 
 
    { scenario: "Turnover", diagnostic: "Improve trade efficiency of your business", action: "Valorize your labels"}, 
 
    { scenario: "Turnover", diagnostic: "Improve trade efficiency of your business", action: "Analyze the opportunity"}, 
 
    { scenario: "Turnover", diagnostic: "Improve trade efficiency of your firm", action: "Contacter un prestataire"} 
 
]; 
 

 

 
function collectGroupedScenarioDiagnostics(collector, diagnosticItem/*, idx, list*/) { 
 
    var 
 
    scenarioKey   = diagnosticItem.scenario, 
 
    diagnosticKey   = diagnosticItem.diagnostic, 
 

 
    groupedDiagnosticKey = [scenarioKey, diagnosticKey].join(' : '), 
 

 
    scenarioItem   = collector.scenarioStore[scenarioKey], 
 
    groupedDiagnosticItem = collector.diagnosticStore[groupedDiagnosticKey]; 
 

 
    if (!scenarioItem) { 
 
    scenarioItem   = collector.scenarioStore[scenarioKey] = { 
 

 
     scenario : scenarioKey, 
 
     diagnostics : [] 
 
    }; 
 
    collector.diagnosticsList.push(scenarioItem); 
 
    } 
 
    if (!groupedDiagnosticItem) { 
 
    groupedDiagnosticItem = collector.diagnosticStore[groupedDiagnosticKey] = { 
 

 
     diagnostic: diagnosticKey, 
 
     actions : [] 
 
    }; 
 
    scenarioItem.diagnostics.push(groupedDiagnosticItem); 
 
    } 
 
    groupedDiagnosticItem.actions.push(diagnosticItem.action); 
 

 
    return collector; 
 
} 
 

 

 
var groupedScenarioDiagnostics = diagnosticData.reduce(collectGroupedScenarioDiagnostics, { 
 

 
    scenarioStore : {}, 
 
    diagnosticStore : {}, 
 
    diagnosticsList : [] 
 

 
}).diagnosticsList; 
 

 

 
console.log('groupedScenarioDiagnostics : ', groupedScenarioDiagnostics);

相關問題