3 回答
TA贡献1845条经验 获得超8个赞
只使用本机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);
TA贡献1811条经验 获得超4个赞
Array.prototype.filter()Array.prototype.some().
result1result2):
//Find values that are in result1 but not in result2var uniqueResultOne = result1.filter(function(obj) {
return !result2.some(function(obj2) {
return obj.value == obj2.value;
});});//Find values that are in result2 but not in result1var uniqueResultTwo = result2.filter(function(obj) {
return !result1.some(function(obj2) {
return obj.value == obj2.value;
});});//Combine the two arrays of unique entriesvar result = uniqueResultOne.concat(uniqueResultTwo);TA贡献1891条经验 获得超3个赞
$$hashKey
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"}]var
makeSymmDiffFunc = (function() {
var contains = function(pred, a, list) {
var idx = -1, len = list.length;
while (++idx < len) {if (pred(a, list[idx])) {return true;}}
return false;
};
var complement = function(pred, a, b) {
return a.filter(function(elem) {return !contains(pred, elem, b);});
};
return function(pred) {
return function(a, b) {
return complement(pred, a, b).concat(complement(pred, b, a));
};
};}());var myDiff = makeSymmDiffFunc(function(x, y) {
return x.value === y.value && x.display === y.display;});var result = myDiff(a, b);
//=> {value="a63a6f77-c637-454e-abf2-dfb9b543af6c", display="Ryan"}curry
解释
makeSymmDiffFunc:
function(x, y) {
return x.value === y.value && x.display === y.display;}truefalsemakeSymmDiffFunctruefalse
makeSymmDiffFunc
return function(a, b) {
return complement(pred, a, b).concat(complement(pred, b, a));
};some:
var makeSymmDiffFunc = (function() {
var complement = function(pred, a, b) {
return a.filter(function(x) {
return !b.some(function(y) {return pred(x, y);});
});
};
return function(pred) {
return function(a, b) {
return complement(pred, a, b).concat(complement(pred, b, a));
};
};}());complementcontains
complement
更新,几年后
const diffBy = (pred) => (a, b) => a.filter(x => !b.some(y => pred(x, y)))const makeSymmDiffFunc = (pred) => (a, b) =>
diffBy(pred)(a, b).concat(diffBy(pred)(b, a))const myDiff = makeSymmDiffFunc((x, y) => x.value === y.value && x.display
=== y.display)const result = myDiff(a, b)//=> {value="a63a6f77-c637-454e-abf2-dfb9b543af6c", display="Ryan"}添加回答
举报
