比较两个数组对象并使用函数javascript获取更改的对象

时间:2018-03-10 08:13:48

标签: javascript arrays for-loop functional-programming

我试图根据workscopeThresholdcustomerThreshd

来区分两个数组对象
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"
            }
          ]
        }
      ]
    }

如果在不同的数组对象中更改了customerThresholdworkscopeThreshold,则会产生预期结果。 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"
            }
          ]
        }
      ]
    }

1 个答案:

答案 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))