JavaScript比较两个数组(键/值对),如果键匹配,则将值从一个复制到另一个

时间:2013-09-19 19:38:03

标签: javascript

我有两个包含键/值对的数组。

{
    "containerOne": [{
        "Id": 1,
            "Title": "TitleOne",
            "Responsibility": "ValueOne"
    }, {
        "Id": 2,
            "Title": "TitleTwo",
            "Responsibility": "ValueTwo"
    }]
}

{
    "containerTwo": [{
        "Id": 1,
            "Title": "TitleOne",
            "Responsibility": null
    }, {
        "Id": 2,
            "Title": "TitleTwo",
            "Responsibility": "null         
                               }
            ]
    }

我想比较两个数组并比较每个容器的标题。如果标题匹配,那么我想将“责任”值从containerOne复制到containerTwo。 ID不匹配,因此可以排除。只有标题才一致。

请问最有效的方法是什么?]

由于

=============================================== ======================

修改

=============================================== ======================

仔细观察数组,返回的数据存在细微差别:

{
"AMLookupTasksList":
    [
        {
            "Id":1,
            "Title":"Create and Maintain an Onboarding Document",
            "Responsibility":"1. Onboarding|f101ccf1-c7d5-42e7-ba8f-48e88ac90a3d"
        },
        {
            "Id":2,
            "Title":"Execute Onboarding for New Consultants",
            "Responsibility":"1. Onboarding|f101ccf1-c7d5-42e7-ba8f-48e88ac90a3d"
        }
    ]

}

{
"AMTasksList":
    [
        {
            "Id":4,
            "Title":
                {
                    "$M_1":13,"$c_1":"Create and Maintain an Onboarding Document"
                },
            "Responsibility":null
        },
        {
            "Id":17,
            "Title":
                {
                    "$M_1":12,"$c_1":"Execute Onboarding for New Consultants"
                },
            "Responsibility":null
        }
    ]

}

我是否有额外的循环来获取第二个数组中的Title值?

3 个答案:

答案 0 :(得分:1)

首先,从containerTwo

创建一个字典
var c2dict = {};
var c2i = containerTwo.innerContainer;
for (var i = 0; i < c2i.length; i++) {
    c2dict[c2i[i].Title] = c2i[i];
}

现在使用它来在标题匹配时复制属性:

var c1i = containerOne.innerContainer;
for (var i = 0; i < c1i.length; i++) {
    if (c2dict[c1i[i].Title]) {
        c2dict[c1i[i].Title].Property = c1i[i].Property;
    }
}

答案 1 :(得分:1)

这可能有点过分但它会忽略序列并在每个对象中查找。 我必须在你的对象中修复一些语法,但我包括:命名对象并从其中一个空值中引用一个引号。

var obj1 = {
    "containerOne": [{
        "Id": 1,
            "Title": "TitleOne",
            "Responsibility": "ValueOne"
    }, {
        "Id": 2,
            "Title": "TitleTwo",
            "Responsibility": "ValueTwo"
    }]
};
var obj2 = {
    "containerTwo": [{
        "Id": 1,
            "Title": "TitleOne",
            "Responsibility": null
    }, {
        "Id": 2,
            "Title": "TitleTwo",
            "Responsibility": null
    }]
};

现在代码:

// lookup for first object:
var lookup = {};
// create referece to list above and use it everywhere
lookup.list = obj1;
for (var i = 0, len = lookup.list.containerOne.length; i < len; i++) {
    lookup[lookup.list.containerOne[i].Title] = lookup.list.containerOne[i];
}
// lookup for second object
var otherLookup = {};
otherLookup.list = obj2;
for (var i = 0, len = otherLookup.list.containerTwo.length; i < len; i++) {
    otherLookup[otherLookup.list.containerTwo[i].Title] = otherLookup.list.containerTwo[i];
}

// copy value for Responsibility from first to second on each matched in second.
var i = 0;
var len = lookup.list.containerOne.length;
for (i; i < len; i++) {
    // looks up value from second list in the first one and if found, copies 
    if (lookup[otherLookup.list.containerTwo[i].Title]) {
        otherLookup.list.containerTwo[i].Responsibility = lookup[otherLookup.list.containerTwo[i].Title].Responsibility;
    }
}

// alerts new value using lookup
alert(otherLookup["TitleOne"].Responsibility);

为新结构编辑,但确实有相同的答案:

var obj1 = {
    "AMLookupTasksList": [{
        "Id": 1,
            "Title": "Create and Maintain an Onboarding Document",
            "Responsibility": "1. Onboarding|f101ccf1-c7d5-42e7-ba8f-48e88ac90a3d"
    }, {
        "Id": 2,
            "Title": "Execute Onboarding for New Consultants",
            "Responsibility": "1. Onboarding|f101ccf1-c7d5-42e7-ba8f-48e88ac90a3d"
    }]
};
var obj2 = {
    "AMTasksList": [{
        "Id": 4,
            "Title": {
            "$M_1": 13,
                "$c_1": "Create and Maintain an Onboarding Document"
        },
            "Responsibility": null
    }, {
        "Id": 17,
            "Title": {
            "$M_1": 12,
                "$c_1": "Execute Onboarding for New Consultants"
        },
            "Responsibility": null
    }]
};

var lookup = {};
// create refernece to list above and use it everywhere
lookup.list = obj1;
for (var i = 0, len = lookup.list.AMLookupTasksList.length; i < len; i++) {
    lookup[lookup.list.AMLookupTasksList[i].Title] = lookup.list.AMLookupTasksList[i];
}
var otherLookup = {};
otherLookup.list = obj2;
for (var i = 0, len = otherLookup.list.AMTasksList.length; i < len; i++) {
    otherLookup[otherLookup.list.AMTasksList[i].Title.$c_1] = otherLookup.list.AMTasksList[i];
}

// copy value for Responsibility from first to second
var i = 0;
var len = otherLookup.list.AMTasksList.length;
for (i; i < len; i++) {
    if (lookup[otherLookup.list.AMTasksList[i].Title.$c_1]) {

        otherLookup.list.AMTasksList[i].Responsibility = lookup[otherLookup.list.AMTasksList[i].Title.$c_1].Responsibility;
    }
}

alert(otherLookup["Create and Maintain an Onboarding Document"].Responsibility);

小提示第二个答案:http://jsfiddle.net/n22V8/

答案 2 :(得分:0)

您应该比较属性并将其设置为以下内容:

containerOne.innerContainer.forEach(function (element,index) {
    containerTwo.innerContainer.forEach(function (element2,index2) {
       if (element.Title === element2.Title && element.Property != element2.Property) {
           element2.Property = element.Property;
       }
    });
});