1

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.

2 Answers 2

1

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) );
Sign up to request clarification or add additional context in comments.

Comments

0

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)
  )
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.