如果该数组中的所有对象都不匹配第二个数组,则需要比较两个数组并将元素推入一个数组。
请在下面找到我的JSON:
阵列1:
"[{
"carrierName":"A",
"id":7,
"active":true
"subList":[{
"active":false,
"carrierServiceId":"19",
"locationPrimaryContactName":"ABC 4",
"locationPrimaryContactNumber":"1111",
"locationStateCode":"NJ",
"locationZipcode":"56324",
"name":"A 4"}
,
{
"active":true,
"carrierServiceId":"20",
"locationPrimaryContactName":"ABC 1",
"locationPrimaryContactNumber":"1111",
"locationStateCode":"NJ",
"locationZipcode":"56324",
"name":"A 1"
},
{
"active":true,
"carrierServiceId":"21",
"locationPrimaryContactName":"ABC 2",
"locationPrimaryContactNumber":"1111",
"locationStateCode":"NJ",
"locationZipcode":"56324",
"name":"A 2"},
{
"active":true,
"carrierServiceId":"22",
"id":1001,
"locationPrimaryContactName":"ABC 3",
"locationPrimaryContactNumber":"1111",
"locationStateCode":"NJ",
"locationZipcode":"56324",
"name":"A 3"
}
]
}
]"
阵列2:
"[{"subList":[
{
"active":true,
"code":"20",
"id":0,
"locationPrimaryContactName":"ABC 1",
"locationPrimaryContactNumber":"1111",
"name":"A 1",
},
{
"active":true,
"code":"21",
"id":0,
"locationPrimaryContactName":"ABC 2",
"locationPrimaryContactNumber":"1111",
"name":"A 2",
},
{
"active":true,
"code":"22",
"id":0,
"locationPrimaryContactName":"ABC 3",
"locationPrimaryContactNumber":"1111",
"name":"A 3",
},
{
"active":true,
"code":"19",
"id":0,
"locationPrimaryContactName":"ABC 4",
"locationPrimaryContactNumber":"1111",
"name":"A 4",
}
],
"active":false,
"id":7,
"name":"A",
},
{
"subList":[
{
"active":true,
"code":"7",
"id":0,
"locationPrimaryContactName":"DEF 1",
"locationPrimaryContactNumber":"2222",
"name":"B 1",
},
{
"active":true,
"code":"8",
"id":0,
"locationPrimaryContactName":"DEF 2",
"locationPrimaryContactNumber":"2222",
"name":"B 2",
},
{
"active":true,
"code":"9",
"id":0,
"locationPrimaryContactName":"DEF 3",
"locationPrimaryContactNumber":"2222",
"name":"B 3",
}
],
"active":false,
"id":8,
"name":"B",
},
{
"subList":[
{
"active":true,
"code":"10",
"locationPrimaryContactName":"GHI 1",
"locationPrimaryContactNumber":"3333",
"name":"C 1",
}
],
"active":false,
"id":9,
"name":"C",
},
{
"subList":[
],
"active":false,
"id":10,
"name":"D",
}
]"
在这里,如果看到的话,公共字段是id,即数组1中的sublist.carrierServiceId和数组2中的subList.code。我的要求是我需要遍历数组1和数组2,并将数组2中的元素添加到数组1中。如果不存在,则为数组1。添加时,还需要检查“ subList”数组中数组2中是否有任何特定的“ id”,如果数组1中存在“ id”,则也不应添加它们。
请求任何专家帮助。请让我知道查询是否需要更多的清晰度。
如果我理解正确,则需要在正确的位置将数组2的所有元素放入数组1。
这可以参考给定的对象,并在必要时创建一个新对象。
var array1 = [{ carrierName: "A", id: 7, active: true, subList: [{ active: false, carrierServiceId: "19", locationPrimaryContactName: "ABC 4", locationPrimaryContactNumber: "1111", locationStateCode: "NJ", locationZipcode: "56324", name: "A 4" }, { active: true, carrierServiceId: "20", locationPrimaryContactName: "ABC 1", locationPrimaryContactNumber: "1111", locationStateCode: "NJ", locationZipcode: "56324", name: "A 1" }, { active: true, carrierServiceId: "21", locationPrimaryContactName: "ABC 2", locationPrimaryContactNumber: "1111", locationStateCode: "NJ", locationZipcode: "56324", name: "A 2" }, { active: true, carrierServiceId: "22", id: 1001, locationPrimaryContactName: "ABC 3", locationPrimaryContactNumber: "1111", locationStateCode: "NJ", locationZipcode: "56324", name: "A 3" }] }],
array2 = [{ subList: [{ active: true, code: "20", id: 0, locationPrimaryContactName: "ABC 1", locationPrimaryContactNumber: "1111", name: "A 1", }, { active: true, code: "21", id: 0, locationPrimaryContactName: "ABC 2", locationPrimaryContactNumber: "1111", name: "A 2", }, { active: true, code: "22", id: 0, locationPrimaryContactName: "ABC 3", locationPrimaryContactNumber: "1111", name: "A 3", }, { active: true, code: "19", id: 0, locationPrimaryContactName: "ABC 4", locationPrimaryContactNumber: "1111", name: "A 4", }], active: false, id: 7, name: "A", }, { subList: [{ active: true, code: "7", id: 0, locationPrimaryContactName: "DEF 1", locationPrimaryContactNumber: "2222", name: "B 1", }, { active: true, code: "8", id: 0, locationPrimaryContactName: "DEF 2", locationPrimaryContactNumber: "2222", name: "B 2", }, { active: true, code: "9", id: 0, locationPrimaryContactName: "DEF 3", locationPrimaryContactNumber: "2222", name: "B 3", }], active: false, id: 8, name: "B", }, { subList: [{ active: true, code: "10", locationPrimaryContactName: "GHI 1", locationPrimaryContactNumber: "3333", name: "C 1", }], active: false, id: 9, name: "C", }, { subList: [], active: false, id: 10, name: "D" }],
reference = {};
array1.forEach(function (a) {
if (!reference[a.id]) {
reference[a.id] = { data: a, children: {} };
}
a.subList.forEach(function (b) {
reference[a.id].children[b.carrierServiceId] = b;
});
});
array2.forEach(function (a) {
if (!reference[a.id]) {
reference[a.id] = { data: { carrierName: a.name, id: a.id, active: a.active, subList: [] }, children: {} };
array1.push(reference[a.id].data);
}
a.subList.forEach(function (b) {
if (!reference[a.id].children[b.code]) {
reference[a.id].children[b.code] = {
active: b.active,
carrierServiceId: b.code,
locationPrimaryContactName: b.locationPrimaryContactName,
locationPrimaryContactNumber: b.locationPrimaryContactNumber,
// locationStateCode: b.locationPrimaryContactNumber,
// locationZipcode: b.locationZipcode,
name: b.name
};
reference[a.id].data.subList.push(reference[a.id].children[b.code]);
}
});
});
console.log(array1);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句