0

I want to compare 2 Arrays of Objects to find the object or objects which is not matched. In the example below it should output {label: 'Addition', type: 'address', name: 'address_4', defaultValue: 'test'} as this Object is not matched.

const A = [{
    label: 'Street Name',
    type: 'address',
    name: 'address_1',
    defaultValue: 'test1'
  },
  {
    label: 'House Number',
    type: 'address',
    name: 'address_2',
    defaultValue: '1563l1'
  },
  {
    label: 'Addition',
    type: 'address',
    name: 'address_3',
    defaultValue: 'ABC684'
  }
]

const B = [{
    label: 'Street Name',
    type: 'address',
    name: 'address_1',
    defaultValue: 'test1'
  },
  {
    label: 'House Number',
    type: 'address',
    name: 'address_2',
    defaultValue: '1563l1'
  },
  {
    label: 'Addition',
    type: 'address',
    name: 'address_3',
    defaultValue: 'ABC684'
  },
  {
    label: 'Addition',
    type: 'address',
    name: 'address_4',
    defaultValue: 'test'
  }
]

const newSet = A;
const currentSet = B;

let difference = currentSet.filter((page1) => !newSet.find(page2 => page1.name === page2.name))

console.log(difference);

This does not work. It outputs an empty Array. What I'm doing wrong?

danh
  • 62,181
  • 10
  • 95
  • 136
nhatimme
  • 383
  • 3
  • 19
  • I think there is a similar question: https://stackoverflow.com/questions/31005396/filter-array-of-objects-with-another-array-of-objects. Hope this helps – k-code Dec 06 '21 at 16:00
  • Does this answer your question? [How to get the difference between two arrays of objects in JavaScript](https://stackoverflow.com/questions/21987909/how-to-get-the-difference-between-two-arrays-of-objects-in-javascript) – pilchard Dec 06 '21 at 16:00
  • The snippet introduced to undefined symbols `newSet` and `currentSet`. I've just edited to assign them to A and B respectively, causing the code to run as expected. I think @decpk might have correctly guessed a diagnosis that the OP mixed up the two arrays being tested. – danh Dec 06 '21 at 16:17

1 Answers1

0

You might by comparing A with B which returns an empty array because there is no element in A which doesn't exist in B instead you should compare B with A.

Below code will return an empty array

const A = [
  {
    label: "Street Name",
    type: "address",
    name: "address_1",
    defaultValue: "test1",
  },
  {
    label: "House Number",
    type: "address",
    name: "address_2",
    defaultValue: "1563l1",
  },
  {
    label: "Addition",
    type: "address",
    name: "address_3",
    defaultValue: "ABC684",
  },
];

const B = [
  {
    label: "Street Name",
    type: "address",
    name: "address_1",
    defaultValue: "test1",
  },
  {
    label: "House Number",
    type: "address",
    name: "address_2",
    defaultValue: "1563l1",
  },
  {
    label: "Addition",
    type: "address",
    name: "address_3",
    defaultValue: "ABC684",
  },
  {
    label: "Addition",
    type: "address",
    name: "address_4",
    defaultValue: "test",
  },
];

let difference = A.filter((page1) => {
  const result = B.find((page2) => page1.name === page2.name);
  return !result;
});

console.log(difference);

You should compare B with A as:

const A = [
  {
    label: "Street Name",
    type: "address",
    name: "address_1",
    defaultValue: "test1",
  },
  {
    label: "House Number",
    type: "address",
    name: "address_2",
    defaultValue: "1563l1",
  },
  {
    label: "Addition",
    type: "address",
    name: "address_3",
    defaultValue: "ABC684",
  },
];

const B = [
  {
    label: "Street Name",
    type: "address",
    name: "address_1",
    defaultValue: "test1",
  },
  {
    label: "House Number",
    type: "address",
    name: "address_2",
    defaultValue: "1563l1",
  },
  {
    label: "Addition",
    type: "address",
    name: "address_3",
    defaultValue: "ABC684",
  },
  {
    label: "Addition",
    type: "address",
    name: "address_4",
    defaultValue: "test",
  },
];

let difference = B.filter((page1) => {
  const result = A.find((page2) => page1.name === page2.name);
  return !result;
});

console.log(difference);

If there is a case where you want to get the difference between two array then you can do as:

const [small, large] = A.length < B.length ? [A, B] : [B, A];
let difference = large.filter((page1) => {
  const result = small.find((page2) => page1.name === page2.name);
  return !result;
});
DecPK
  • 24,537
  • 6
  • 26
  • 42