Reputation: 1184
I have a problem to get the diffrent element from 2 array object. my example is just like this:
array1= [{id: 1, a: "a", b: "b"}, {id: 2, c: "c", d: "d"}, {id: 3, e: "e", f: "f"}];
array2 = [{c: "c", d: "d"}];
what I expected is:
the output Result is just like this:
this id in every object is not relevant.
result =[{a: "a", b: "b"},{e: "e", f: "f"}];
I have try to use filter or find in typescript, but is was not working.
any solutions??
Upvotes: 0
Views: 58
Reputation: 22758
Using lodash
package and its isEqual
method it may look like this:
const _ = require('lodash');
array1= [{id: 1, a: "a", b: "b"}, {id: 2, c: "c", d: "d"}, {id: 3, e: "e", f: "f"}];
array2 = [{c: "c", d: "d"}];
const results = array1.filter(item => !array2.some(item2 => _.isEqual(_.omit(item, ['id']), _.omit(item2, ['id']))))
That way you can compare objects with more that one level of props
Upvotes: 2
Reputation: 8751
array1= [{a: "a", b: "b"}, {c: "c", d: "d"}, {e: "e", f: "f"}];
array2 = [{c: "c", d: "d"}];
const result = array1.filter(subary => {
var flag = false;
if (Object.keys(subary).length == Object.keys(array2[0]).length) {
for (key in subary) {
if (subary[key] == array2[0][key]) {
continue;
} else {
flag = true;
break;
}
}
} else {
flag = true;
}
return flag;
})
console.log(result);
Upvotes: 0