我试图重新组合对象的复杂数组。

这是我的数组:

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

最佳答案

您可以使用迭代方法,并为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; }

07-25 23:03
查看更多