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.

5
  • you have different objects. only objects with same object reference is equal. Commented Jun 15, 2021 at 8:14
  • @NinaScholz So how exactly can I compare two array of objects? I couldn't find anything related to this. Commented Jun 15, 2021 at 8:19
  • 1
    Does this answer your question? How to get the difference between two arrays of objects in JavaScript Commented Jun 15, 2021 at 8: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? Commented Jun 15, 2021 at 10:06
  • @Bladeski Thank you for the suggestion. It guided me to exactly what I wanted. Commented Jun 15, 2021 at 11:49

4 Answers 4

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; }

Sign up to request clarification or add additional context in comments.

2 Comments

It gives all the element present in array2, I only want the element which are missing in array2 when comparing with array1
Thank you for your solution. Exactly what I wanted. :)
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).

4 Comments

Thank you for your response. But when I am applying your code, on console.log(absent), it's giving me empty array, instead of missing element.
What is the response you expect in your answer?
I have added the expected O/P. Please let me know if you need any further details.
I have updated the answer based on your input.
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);

Comments

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

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.