function equals(a, b){
return (a.name === b.name && a.type === b.type);
}
function has(arr, obj){
var len = arr.length;
for(var i = 0; i < arr.length; i++){
if(equals(arr[i], obj)) return true;
}
return false;
}
function clone(obj){
return {
name: obj.name,
type: obj.type
};
}
function intersect(a, b){
var common = [];
if(!a.length || !b.length) return common;
var aLen = a.length;
for(var i = 0; i < aLen; i++){
if(has(b, a[i])){
common.push(clone(a[i]));
}
}
return common;
}
function subtract(a, b){
var result = [];
if(!a.length || !b.length) return result;
var common = intersect(a, b);
var aLen = a.length;
for(var i = 0; i < aLen; i++){
if(!has(common, a[i])){
result.push(clone(a[i]));
}
}
return result;
}
var arr1 = [{ name : 'test1' , type : 'test2' },{ name : 'test2' , type : 'test3' }];
var arr2 = [{ name : 'test1' , type : 'test2' }, { name : 'test3' , type : 'test4' }];
console.log(intersect(arr1, arr2));
console.log(subtract(arr1, arr2));
console.log(subtract(arr2, arr1));