使用multiple和complexe对象对数组项进行分组

时间:2017-01-23 17:13:26

标签: javascript arrays data-structures group-by reduce

我正在尝试重新组合一个复杂的对象数组。

这是我的阵列:

[ 
  { 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统一我的数组,但是我没有得到预期的结果,那么获得一个没有重复项目数组的最有效方法是什么。

3 个答案:

答案 0 :(得分: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)

您可以执行以下操作:;



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));




答案 2 :(得分:-1)

还可以考虑实现一个仅使用查找到本地注入引用的唯一收集器函数,因此,在单个reduce迭代/循环中应用它,它已经聚合/创建了所需的数据结构。

实际上不需要将reduce周期嵌套到forEach迭代中,正如Nina的方法所证明的那样,这种方法也很容易使用后者{{1} }}。 (不仅仅是因为这很难阅读)。

Redush的方法使用thisArgs,但是在每个迭代步骤中都会有一个reduce也是一个迭代器......最终结果依赖于另外两次迭代,{{ 1}}和find

人类可读的示例代码......



keys