合并基于一个关键属性(多)JavaScript对象的数组 [英] Merge an array of Javascript objects based on a Key Property (multilevel)

查看:234
本文介绍了合并基于一个关键属性(多)JavaScript对象的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了Javascript对象的数组看起来像这些:

I have an array of Javascript objects that look like these:

data = [{PK: "Country1", Prop1: "true", children:[
                {PK: "State1", Prop1: "true", children:[
                        {PK: "City1", Prop1: "Value1"}]
                }]
        },
        {PK: "Country1", Prop2: "true", children:[
                {PK: "State2", Prop2: "true", children:[
                        {PK: "City2", Prop2: "Value2"}]
                }]
        },
        {PK: "Country1", Prop3: "true", children:[
                {PK: "State2", Prop3: "true", children:[
                        {PK: "City3", Prop3: "Value3"}]
                }]
       }]

和我想根据 PK属性以合并它们。 想想他们为国家,州和城市。目前,每个对象都有一个国家,在其子女财产的状态下它是一个城市。我希望他们能够合并,也就是如果对象有相同的国家,他们的国家将被合并,如果这两个国家都是一样的,他们的城市将在儿童财产相加。然后,如果一个城市有一个属性为prop1,国家应该表明它也有属性,所以在为prop1 =真正的。这也涉及对国家一级。为了更清楚,我想使它看起来像这样:

and I am trying to merge them based on the PK property. Think of them as countries, states, and cities. Currently each object has a country, under its children property is a state and under it is a city. I want them to be merged such that if both objects have the same country, their states will be merged and if both states are the same, their cities will added together in the children property. Then, if a city has a property Prop1, the state should indicate it also has that property, hence the Prop1 = "true". This also follows on the country level. To make it clearer, I am trying to make it look like this:

data = [{PK: "Country1", Prop1: "true", Prop2: "true", Prop3: "true" children:[
                {PK: "State1", Prop1: "true", children:[
                        {PK: "City1", Prop2: "Value1"}]
                },
                {PK: "State2", Prop2: "true", Prop3: "true", children:[
                        {PK: "City2", Prop2: "Value2"},
                        {PK: "City3", Prop3: "Value3"}]
                }]
       }]

我想是这样的this但各地的孩子们对象,它也是对象的数组,我不能把它包装。有人可以帮助我在这或导致我到一个更好的答案。谢谢!

I tried something like this but I can't wrap it around the children object which is also an array of objects. Can someone help me on this or lead me to a better answer. Thanks!

推荐答案

下面是一个使用我已经写较早前进行的 JSON-易于过滤
希望它的正确的东西给你。看到它在这个普拉克

Here is a simple script using a custom merge functionality I've written sometime ago for json-easy-filter Hope it does the right stuff for you. See it in this plunk

var input = ... your data here ...

var groupByPk = function (arr) {
    var res = {};
    arr.forEach(function (node) {
        if (!res[node.PK]) {
            res[node.PK] = [
                node
            ];
        } else {
            res[node.PK].push(node);
        }
    });
    return res;
};

var mergeByPk = function (arr) {
    var groups = groupByPk(arr);
    var groups2 = [];

    for ( var pk in groups) {
        var group = groups[pk];
        var ress = merge.apply(this, group);
        groups2.push(ress.value);
    }
    return groups2;
};

var mergeData = function(data){
    var mergedCountries = mergeByPk(data);
    mergedCountries.forEach(function (country) {
        var mergedStates = mergeByPk(country.children);
        country.children = mergedStates;
        mergedStates.forEach(function (state) {
            var mergedCities = mergeByPk(state.children);
            state.children = mergedCities;
        });
    });
    return mergedCountries;
};

console.log(JSON.stringify(mergeData(input), null, 4));

这篇关于合并基于一个关键属性(多)JavaScript对象的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆