0

Suppose I have two array of object as:

const array1 = [
  { name: 'detail1', title: 'detail1' },
  { name: 'detail2 ', title: 'detail2 ' },
  { name: 'detail3', title: 'detail3' },
  { name: 'detail4', title: 'detail4' },
  { name: 'detail5', title: 'detail5' },
  { name: 'detail6', title: 'detail6' },
  { name: 'detail7', title: 'detail7' }
]

const array2 = [
  { name: 'detail1', title: 'detail1' },
  { name: 'detail2 ', title: 'detail2 ' },
  { name: 'detail3', title: 'detail3' },
  { name: 'detail4', title: 'detail4' },
]

I want to compare two arrays i.e. array1 and array2 and get the missing element of aaray2.

For this I tried as:

var absent = array2.filter(e=>!array1.includes(e));

But I am unable to get missing element of array2.

My expected O/P :

          [ { name: 'detail5', title: 'detail5' },
            { name: 'detail6', title: 'detail6' },
            { name: 'detail7', title: 'detail7' }]

These are all the elements which are not present in array2.

What exactly am I doing wrong here?

Please let me know if anyone needs any further information.

Siva Pradhan
  • 791
  • 1
  • 6
  • 23
  • you have different objects. only objects with same object reference is equal. – Nina Scholz Jun 15 '21 at 08:14
  • @NinaScholz So how exactly can I compare two array of objects? I couldn't find anything related to this. – Siva Pradhan Jun 15 '21 at 08:19
  • 1
    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) – Bladeski Jun 15 '21 at 08:40
  • When are two objects considered equal in your scenario? Do all the key/value-pairs of an object have to be equal? Or is a comparison based on the name and/or title of an object enough? – 3limin4t0r Jun 15 '21 at 10:06
  • @Bladeski Thank you for the suggestion. It guided me to exactly what I wanted. – Siva Pradhan Jun 15 '21 at 11:49

4 Answers4

2

You could build a normalised object with key and values and filter the objects.

const
    array1 = [{ name: 'detail1', title: 'detail1' }, { name: 'detail2 ', title: 'detail2 ' }, { name: 'detail3', title: 'detail3' }, { name: 'detail4', title: 'detail4' }, { name: 'detail5', title: 'detail6' }, { name: 'detail7', title: 'detail7' }, { name: 'detail8', title: 'detail8' }],
    array2 = [{ name: 'detail1', title: 'detail1' }, { name: 'detail2 ', title: 'detail2 ' }, { name: 'detail3', title: 'detail3' }, { name: 'detail4', title: 'detail4' }],
    sortEntriesByKey = ([a], [b]) => a.localeCompare(b),
    filter = array2.reduce((r, o) => {
        Object
            .entries(o)
            .sort(sortEntriesByKey)
            .reduce((o, [k, v]) => (o[k] ??= {})[v] ??= {}, r);
        return r;
    }, {});
    absent = array1.filter((o) => {
        let f = filter;
        return !Object
            .entries(o)
            .sort(sortEntriesByKey)
            .every(([k, v]) => f = f[k]?.[v]);
    });

console.log(absent);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

Edit: you want objects in A not in B. It is ideal to loop through A, find if the element exists in B. If yes, then do not include it.

In javacript object references are compared when you do "==" or "===" or other array search methods.

{} == {} will return false. You can check in your dev console.

In your case, you will have to check specific properties.

var absent = array1.filter(e=>{
  let findInd = array2.findIndex((a)=>{
    return (a.name == e.name && a.title == e.title);});
return (findInd == -1); });

In the inner findIndex, I am finding index based on a condition. In filter method, I am returning true only if that index is -1(not found).

Tushar Shahi
  • 16,452
  • 1
  • 18
  • 39
0

This worked for me:

let cart = [{
    "id": "6304a51af5726921c0dadd64",
    "qty": 1
  },
  {
    "id": "8704a51af5726921c0dadd64",
    "qty": 1
  },
  {
    "id": "4704a51af5726921c0dadd64",
    "qty": 1
  }
]

let cartList = [{
    "id": "6304a51af5726921c0dadd64",
    "qty": 1
  },
  {
    "id": "9704a51af5726921c0dadd64",
    "qty": 1
  }
]


let test = cart.some((element) =>
  cartList.some((e) => element.id === e.id)
);
console.log(" if any single object matched:", test);

let test1 = cart.filter((element) =>
  cartList.some((e) => element.id === e.id)
);
console.log("display matching objects :", test1);
Oleg Barabanov
  • 2,468
  • 2
  • 8
  • 17
DevS
  • 1
0

const array1 = [
  { name: 'detail1', title: 'detail1' },
  { name: 'detail2 ', title: 'detail2 ' },
  { name: 'detail3', title: 'detail3' },
  { name: 'detail4', title: 'detail4' },
  { name: 'detail5', title: 'detail5' },
  { name: 'detail6', title: 'detail6' },
  { name: 'detail7', title: 'detail7' }
]

const array2 = [
  { name: 'detail1', title: 'detail1' },
  { name: 'detail2 ', title: 'detail2 ' },
  { name: 'detail3', title: 'detail3' },
  { name: 'detail4', title: 'detail4' },
]
const thirdArray = array1.filter((elem) => {
       return !array2.some((ele) => {
       return elem.name === ele.name
         });
       });
Faruk
  • 9
  • 2