一尘不染

如何在JavaScript中获得两个对象数组之间的差异

javascript

我有两个这样的结果集:

// Result 1
[
    { value="0", display="Jamsheer" },
    { value="1", display="Muhammed" },
    { value="2", display="Ravi" },
    { value="3", display="Ajmal" },
    { value="4", display="Ryan" }
]

// Result 2
[
    { value="0", display="Jamsheer" },
    { value="1", display="Muhammed" },
    { value="2", display="Ravi" },
    { value="3", display="Ajmal" },
]

我需要的最终结果是这些数组之间的差异–最终结果应如下所示:

[{ value="4", display="Ryan" }]

是否可以在JavaScript中执行类似的操作?


阅读 442

收藏
2020-04-25

共1个答案

一尘不染

仅使用本机JS,类似的方法将起作用:

a = [{ value:"4a55eff3-1e0d-4a81-9105-3ddd7521d642", display:"Jamsheer"}, { value:"644838b3-604d-4899-8b78-09e4799f586f", display:"Muhammed"}, { value:"b6ee537a-375c-45bd-b9d4-4dd84a75041d", display:"Ravi"}, { value:"e97339e1-939d-47ab-974c-1b68c9cfb536", display:"Ajmal"},  { value:"a63a6f77-c637-454e-abf2-dfb9b543af6c", display:"Ryan"}]

b = [{ value:"4a55eff3-1e0d-4a81-9105-3ddd7521d642", display:"Jamsheer", $$hashKey:"008"}, { value:"644838b3-604d-4899-8b78-09e4799f586f", display:"Muhammed", $$hashKey:"009"}, { value:"b6ee537a-375c-45bd-b9d4-4dd84a75041d", display:"Ravi", $$hashKey:"00A"}, { value:"e97339e1-939d-47ab-974c-1b68c9cfb536", display:"Ajmal", $$hashKey:"00B"}]



function comparer(otherArray){

  return function(current){

    return otherArray.filter(function(other){

      return other.value == current.value && other.display == current.display

    }).length == 0;

  }

}



var onlyInA = a.filter(comparer(b));

var onlyInB = b.filter(comparer(a));



result = onlyInA.concat(onlyInB);



console.log(result);
2020-04-25