0

I want the differences between the two arrays. The result I'm seeking for this example would be {n:"Rice", q: 5}. Also, how would I only get specifically only "Rice"? I don't really care about the q:

let arr = [
{n:"Chicken", q: 10},
{n: "Onion", q: 2},
{n:"Fish", q: 12},]

let other = [
{n:"Chicken", q: 24},
{n: "Onion", q: 8},
{n:"Fish", q: 9},
{n:"Rice", q: 5},]

To start with, I tried using this code... but it did the complete opposite, it grabbed everything else but "Rice".

 let difference = arr.filter(i=>other.find(x=>(x.n !== i.n)));
Ian
  • 39
  • 3
  • You can add `!` in front of `other` to "flip" the conditon/return value, you might also want to consider using `.some()` instead of `.find()` for this. – Nick Parsons Sep 24 '20 at 10:45
  • You also need to be filtering the longer of the two arrays, otherwise you will get an empty array back. – pilchard Sep 24 '20 at 10:57
  • `let difference = other.filter(i => !arr.some(x => (x.n === i.n))).concat(arr.filter(i => !other.some(x => (x.n === i.n))));` – angel.bonev Sep 24 '20 at 11:00
  • clever `concat` but you are doubling the number of iterations necessary. – pilchard Sep 24 '20 at 11:11

1 Answers1

1

Two things.

  1. You need to filter the longer of the two arrays otherwise you risk returning an empty array.

  2. You want to test for whether the second array doesn't contain an object that does have the same n.

So

!arr.find(s => s.n === l.n)

instead of

arr.find(s => s.n !== l.n)

which will match the first n that is not the same as the n you are looking for.


As noted in the comments you can use some() here which

tests whether at least one element in the array passes the test implemented by the provided function. It returns a Boolean value.

though find() is also adequate in this situation.

let arr = [
{n:"Chicken", q: 10},
{n: "Onion", q: 2},
{n:"Fish", q: 12},]

let other = [
{n:"Chicken", q: 24},
{n: "Onion", q: 8},
{n:"Fish", q: 9},
{n:"Rice", q: 5},]

const [longer, shorter] = arr.length > other.length ? [arr, other] : [other, arr];

const difference = longer.filter(l => !shorter.some(s => s.n === l.n));

console.log(difference);
pilchard
  • 12,414
  • 5
  • 11
  • 23
  • What will happen in this case `let arr = [{n: "Chicken", q: 10}]; let other = [{n: "TEST", q: 24}]` – angel.bonev Sep 24 '20 at 12:00
  • @angel.bonev It would return `[{n: "TEST", q: 24}]` since the longer/shorter conditional defaults to `other` if they are the same length. The linked duplicate answer handles checking both directions. – pilchard Sep 24 '20 at 12:09