我试图根据workscopeThreshold
和customerThreshd
var array1 = {
"family": "ABC",
"globalThreshold": "2.0",
"spiData": [
{
"customerName": "AIR CANADA",
"customerThreshold": "4.0",
"workscopes": [
{
"id": "21",
"workscopeName": "1 - QT - Quick Turn",
"wokscopeThreshold": "7.0"
},
{
"id": "22",
"workscopeName": "2 - SV - Light",
"wokscopeThreshold": "8.0"
}
]
},
{
"customerName": "AIR CHINA",
"customerThreshold": "8.0",
"workscopes": [
{
"id": "10",
"workscopeName": "1 - QT - Quick Turn",
"wokscopeThreshold": "6.0"
},
{
"id": "11",
"workscopeName": "2 - SV - Light",
"wokscopeThreshold": "7.0"
}
]
}
]
}
var array2 = {
"family": "ABC",
"globalThreshold": "2.0",
"spiData": [
{
"customerName": "AIR CANADA",
"customerThreshold": "4.0",
"workscopes": [
{
"id": "21",
"workscopeName": "1 - QT - Quick Turn",
"wokscopeThreshold": "7.0"
},
{
"id": "22",
"workscopeName": "2 - SV - Light",
"wokscopeThreshold": "8.0"
}
]
},
{
"customerName": "AIR CHINA",
"customerThreshold": "8.0",
"workscopes": [
{
"id": "10",
"workscopeName": "1 - QT - Quick Turn",
"wokscopeThreshold": "6.0"
},
{
"id": "11",
"workscopeName": "2 - SV - Light",
"wokscopeThreshold": "877.0"
}
]
}
]
}
在array2中,我已经将id:11的customerThreshold更改为877.因此,在最终对象中,我希望仅在workscopes
数组的“AIR CHINA”中获取该更改的对象。 ie。删除具有相同customerThreshold
的对象。如果是customerThreshold
,则会使用。方法。
我尝试了如下所示的代码,但它保留了最后一个索引的类似对象。不确定我在哪里做错了。任何具有功能性javascript的解决方案都会非常有用
function remove_duplicates(a, b) {
for (let i = 0, len = a.length; i < len; i++) {
for (let j = 0, len2 = b.length; j < len2; j++) {
if (a[i].id === b[j].id && a[i].customerThreshold === b[j].customerThreshold) {
b.splice(j, 1);
len2 = b.length;
}
}
}
return b;
}
const test = oldObj.spiData.forEach(oldSpi => {
newObj.forEach(newSpi => {
const uniqueWorkscope = remove_duplicates(oldSpi.workscopes, newSpi.workscopes);
return oldSpi.workscopes = uniqueWorkscope;
});
});
预期结果,如果只有&#39; workscopeThreshold&#39;已更改
var expectedResult1 = {
"family": "ABC",
"globalThreshold": "2.0",
"spiData": [
{
"customerName": "AIR CHINA",
"customerThreshold": "8.0",
"workscopes": [
{
"id": "11",
"workscopeName": "2 - SV - Light",
"wokscopeThreshold": "877.0"
}
]
}
]
}
如果在不同的数组对象中更改了customerThreshold
和workscopeThreshold
,则会产生预期结果。
PS:我已将加拿大航空的customerThreshold
改为5756
var expectedResult2= {
"family": "ABC",
"globalThreshold": "2.0",
"spiData": [
{
"customerName": "AIR CANADA",
"customerThreshold": "5756",
"workscopes": []
},
{
"customerName": "AIR CHINA",
"customerThreshold": "8.0",
"workscopes": [
{
"id": "11",
"workscopeName": "2 - SV - Light",
"wokscopeThreshold": "877.0"
}
]
}
]
}
答案 0 :(得分:0)
Array.prototype.reduce()
,Array.prototype.filter()
和JSON.stringify()
可以廉价/功能return
组合嵌套Arrays
新旧Objects
之间的差异。
// Original Data.
const array1 = {
"family": "ABC",
"globalThreshold": "2.0",
"spiData": [
{
"customerName": "AIR CANADA",
"customerThreshold": "4.0",
"workscopes": [
{
"id": "21",
"workscopeName": "1 - QT - Quick Turn",
"wokscopeThreshold": "7.0"
},
{
"id": "22",
"workscopeName": "2 - SV - Light",
"wokscopeThreshold": "8.0"
}
]
},
{
"customerName": "AIR CHINA",
"customerThreshold": "8.0",
"workscopes": [
{
"id": "10",
"workscopeName": "1 - QT - Quick Turn",
"wokscopeThreshold": "6.0"
},
{
"id": "11",
"workscopeName": "2 - SV - Light",
"wokscopeThreshold": "7.0"
}
]
}
]
}
// New Data.
const array2 = {
"family": "ABC",
"globalThreshold": "2.0",
"spiData": [
{
"customerName": "AIR CANADA",
"customerThreshold": "4.0",
"workscopes": [
{
"id": "21",
"workscopeName": "1 - QT - Quick Turn",
"wokscopeThreshold": "7.0"
},
{
"id": "22",
"workscopeName": "2 - SV - Light",
"wokscopeThreshold": "8.0"
}
]
},
{
"customerName": "AIR CHINA",
"customerThreshold": "8.0",
"workscopes": [
{
"id": "10",
"workscopeName": "1 - QT - Quick Turn",
"wokscopeThreshold": "6.0"
},
{
"id": "11",
"workscopeName": "2 - SV - Light",
"wokscopeThreshold": "877.0"
}
]
}
]
}
// Differences.
const workscopeDifferences = (ORIGINAL, NEW) => ({
family: NEW.family,
globalThreshold: NEW.globalThreshold,
spiData: NEW.spiData.reduce((differences, object) => (!JSON.stringify(ORIGINAL.spiData).includes(JSON.stringify(object)) && [...differences, {...object, workscopes: object.workscopes.filter((scope) => !JSON.stringify(ORIGINAL.spiData).includes(JSON.stringify(scope)))}] || differences), [])
})
// Log.
console.log(workscopeDifferences(array1, array2))