0

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

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

我想比较两个数组并比较每个容器的标题。如果标题匹配,那么我想将 Responsibility 值从 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 值?

4

3 回答 3

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;
    }
}
于 2013-09-19T19:51:16.740 回答
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/

于 2013-09-19T21:00:41.200 回答
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;
       }
    });
});
于 2013-09-19T19:45:26.170 回答