Reputation: 3755
How can I find all values in an array A - where the key is not in array B ( a bit like a SQL Not In ) . I don't want to compare the whole array , just one property - but I do want to return all fields for the differences using lodash ( or simpler )
const arrayA = [
{ sku:"1", name:"one"},
{ sku:"2", name:"two"}
]
const arrayB = [
{ sku:"1", name:"One Product"},
{ sku:"2", name:"Two Product"},
{ sku:"3", name:"Three Product"}
]
The results should be :
{ sku:"3", name:"Three Product"}
Thank you for your time.
Upvotes: 1
Views: 45
Reputation: 3333
You could also use:
const arrayA = [
{ sku:"1", name:"one"},
{ sku:"2", name:"two"}
];
const arrayB = [
{ sku:"1", name:"One Product"},
{ sku:"2", name:"Two Product"},
{ sku:"3", name:"Three Product"}
];
let diff = _.differenceWith(arrayB, arrayA, (a, b) => _.isEqual(a.sku, b.sku) );
Upvotes: 1
Reputation: 35501
You don't really need lodash for this. What you need is to filter the second array and eliminate all elements existing in the first array.
const arrayA = [
{ sku:"1", name:"one"},
{ sku:"2", name:"two"}
]
const arrayB = [
{ sku:"1", name:"One Product"},
{ sku:"2", name:"Two Product"},
{ sku:"3", name:"Three Product"}
]
console.log(
// filter B such that we only leave items from B that are not in A
arrayB.filter(b =>
!arrayA.some(a => a.sku === b.sku)
)
)
Upvotes: 0