通过与另一个数组javascript匹配来覆盖数组元素值

时间:2015-11-11 15:30:05

标签: javascript arrays indexof

array1的父级有子级。 array2在array1中几乎没有其他父项子元素。 array1子元素应该由array2中的子元素替换

ARRAY1:

#include <stdio.h>

int main()
{
    __asm
    {
        push 000A6425h ;= format in ascii for %d\n
        push 1; argument to print
        lea edx, dword ptr[esp + 4]; get address of the format on stack
        push edx ; push the address of the format
        call printf
        add esp, 12
    }

    return 0;
}

数组2:

 {
    "name": "USA",
    "id": "country: USA",
    "children": [
        {
            "name": "chicago",
            "id": "country:USA>chicago"
        },
        {
            "name": "arizona",
            "id": "country:USA>arizona"
        },
        {
            "name": "alabama",
            "id": "country:USA>alabama"
        }
    ]
},

    {
        "name": "xyz",
        "id": "country:xyz",
        "children": [
            {
                "name": "name1",
                "id": "country:xyz>name1"
            },
            {
                "name": "name2",
                "id": "country:xyz>name2"
            }
        ]
    },
    {
        "name": "abc",
        "id": "country:abc",
        "children": [
            {
                "name": "name1",
                "id": "country:abc>abc1"
            },
            {
                "name": "name2",
                "id": "country:abc>abc2"
            }
        ]
    }

结果:

[
{
    "name": "name3",
    "id": "country:xyz>name1"
},
{
    "name": "georgia",
    "id": "country:USA>georgia"
},
{
    "name": "name4",
    "id": "country:xyz>name4"
}

我试过但没有工作的代码。我的代码问题是我按原样返回array1,而我希望array2在array1中找到它的父代。它找到父级的那一刻,它应该用array2的数据替换array1子级。

[
{
    "name": "USA",
    "id": "country:USA",
    "children": [
        {
            "name": "georgia",
            "id": "country:USA>georgia"
        }
    ]
},
{
    "name": "xyz",
    "id": "country:xyz",
    "children": [
        {
            "name": "name3",
            "id": "country:xyz>name1"
        },
        {
            "name": "name4",
            "id": "country:xyz>name4"
        }
    ]
},
{
    "name": "abc",
    "id": "country:abc",
    "children": [
        {
            "name": "name1",
            "id": "country:abc>abc1"
        },
        {
            "name": "name2",
            "id": "country:abc>abc2"
        }
    ]
}

}

1 个答案:

答案 0 :(得分:0)

我建议使用分组项的键构建一个临时对象,然后迭代array1,如果属性idtemp对象中,则替换该内容具有新内容的children的属性。

var array1 = [{ "name": "USA", "id": "country:USA", "children": [{ "name": "chicago", "id": "country:USA>chicago" }, { "name": "arizona", "id": "country:USA>arizona" }, { "name": "alabama", "id": "country:USA>alabama" }] }, { "name": "xyz", "id": "country:xyz", "children": [{ "name": "name1", "id": "country:xyz>name1" }, { "name": "name2", "id": "country:xyz>name2" }] }, { "name": "abc", "id": "country:abc", "children": [{ "name": "name1", "id": "country:abc>abc1" }, { "name": "name2", "id": "country:abc>abc2" }] }],
    array2 = [{ "name": "name3", "id": "country:xyz>name1" }, { "name": "georgia", "id": "country:USA>georgia" }, { "name": "name4", "id": "country:xyz>name4" }];

function f() {
    var temp = {};
    array2.forEach(function (a) {
        var key = /(country:.*)>/i.exec(a.id)[1];
        temp[key] = temp[key] || [];
        temp[key].push(a);
    });
    array1.forEach(function (a) {
        if (a.id in temp) {
            a.children = temp[a.id];
        }
    });
}

f();
document.write('<pre>' + JSON.stringify(array1, 0, 4) + '</pre>');