简体   繁体   English

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

[英]JavaScript compare two arrays(key/value pairs) and copy value from one to the other if key matches

I have two arrays containing key/value pairs. 我有两个包含键/值对的数组。

{
    "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         
                               }
            ]
    }

I'd like to compare both arrays and compare the title of each container. 我想比较两个数组并比较每个容器的标题。 If the titles match, then I'd like to copy the Responsibility value from containerOne to containerTwo. 如果标题匹配,那么我想将“责任”值从containerOne复制到containerTwo。 The ID's will not match, so that can be ruled out. ID不匹配,因此可以排除。 Only the titles will be consistent. 只有标题才一致。

What is the most efficient way to do this please?] 请问最有效的方法是什么?]

Thanks 谢谢

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

EDIT 编辑

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

Looking at the arrays a little closer, there is a subtle difference in the data being returned: 仔细观察数组,返回的数据有细微差别:

{
"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
        }
    ]

} }

Do I have additional looping to get to the Title value in the second array? 我是否有额外的循环来获取第二个数组中的Title值?

First, create a dictionary from containerTwo : 首先,从containerTwo创建一个字典:

var c2dict = {};
var c2i = containerTwo.innerContainer;
for (var i = 0; i < c2i.length; i++) {
    c2dict[c2i[i].Title] = c2i[i];
}

Now use this to do the copying of propertyies when titles match: 现在使用它来在标题匹配时复制属性:

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

This might be a bit of overkill but it ignores the sequence and does a look up in each object. 这可能有点过分,但它会忽略序列并在每个对象中查找。 I had to fix some syntax in your objects but I include that: named the objects and took a quote off one of the null values. 我必须在你的对象中修复一些语法,但我包括:命名对象并从其中一个空值中引用一个引号。

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
    }]
};

Now the code: 现在的代码:

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

EDIT for new structure, but same answer really: 编辑新结构,但同样的答案:

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

Fiddle for second answer: http://jsfiddle.net/n22V8/ 小提琴第二个答案: http//jsfiddle.net/n22V8/

You should compare properties and set them as the following: 您应该比较属性并将它们设置为以下内容:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM