1

I'm trying to compare 2 arrays of objects, and am currently doing it by using Array.length() which does not seem to be the right way based on what I'm trying to achieve.

I have these two Arrays of objects:

const array1 = [
  {
    "book_id": 285,
    "status": "Borrowed",
    "attributes": [
      {
        "name": "To Kill a Mockingbird",
        "description": "Published in 1960, this timeless classic explores human behaviour and the collective conscience of The Deep South in the early 20th century. Humour entwines the delicate strands of prejudice, hatred, hypocrisy, love and innocence to create one of the best novels ever written.",
        "borrowed_by": "John"
      }
    ]
  },
  {
    "book_id": 284,
    "status": "Borrowed",
    "attributes": [
      {
        "book_name": "ATTRIBSAMP 1",
        "description": "Although 1984 has passed us by, George Orwell’s dystopian, totalitarian world of control, fear and lies has never been more relevant. Delve into the life of Winston Smith as he struggles with his developing human nature in a world where individuality, freewill and love are forbidden.",
        "borrowed_by": "Bob"
      }
    ]
  }
]

VS

const array2 = [
  {
    "book_id": 285,
    "status": "Free",
    "attributes": [
      {
        "name": "To Kill a Mockingbird",
        "description": "Published in 1960, this timeless classic explores human behaviour and the collective conscience of The Deep South in the early 20th century. Humour entwines the delicate strands of prejudice, hatred, hypocrisy, love and innocence to create one of the best novels ever written.",
        "borrowed_by": ""
      }
    ]
  },
  {
    "book_id": 284,
    "status": "Borrowed",
    "attributes": [
      {
        "book_name": "ATTRIBSAMP 1",
        "description": "Although 1984 has passed us by, George Orwell’s dystopian, totalitarian world of control, fear and lies has never been more relevant. Delve into the life of Winston Smith as he struggles with his developing human nature in a world where individuality, freewill and love are forbidden.",
        "borrowed_by": "Bob"
      }
    ]
  }
]

Basically I want to check whether there are changes between the two, i.e. borrowed_by and/or status, I understand now that array1.length will be equal to array2.length since the array length would always be equal to 2. Is there an easier or better way to check this?

Expected output, I would like to call function callFunction1():

if(compare(array1,array2){
    callFunction1();
} else {
    null;
}

I currently have:

if(array1.length !== array2.length){
    callFunction1();
} else {
    null;
}
mayankmehtani
  • 435
  • 4
  • 14
G Josh
  • 271
  • 1
  • 5
  • 15
  • What would be the expected output? can you add that also. – uiTeam324 Nov 23 '20 at 05:14
  • Edited to add what I want to do. – G Josh Nov 23 '20 at 05:18
  • array1.length and array2.length here are both the same aren't they i.e. both are length 2? You'll always have null being called – mayankmehtani Nov 23 '20 at 05:23
  • @MayankMehtani Yes, that is what I am asking, how do I compare them "better" in order to get to the function call :) – G Josh Nov 23 '20 at 05:24
  • I mean after comparing two arrays what would be your expected output like? You want to compare array1 with array2 or vice versa? – uiTeam324 Nov 23 '20 at 05:25
  • @uiTeam324 If you put it that way, the output would be simply returning ```true``` or ```false``` if there is a difference between the 2 arrays, does not matter what the order is – G Josh Nov 23 '20 at 05:27
  • 4
    You probably want to do a deep compare. Check this SO post https://stackoverflow.com/a/31683111/11321732 – Saihaj Nov 23 '20 at 05:27
  • if you want to compare the properties and values of both object then go through this http://adripofjavascript.com/blog/drips/object-equality-in-javascript.html – Zain Ur Rehman Nov 23 '20 at 05:27
  • Does this answer your question? [How to determine equality for two JavaScript objects?](https://stackoverflow.com/questions/201183/how-to-determine-equality-for-two-javascript-objects) – Brian McCutchon Nov 23 '20 at 05:31
  • @Saihaj using lodash' _.isEqual looks to be the cleanest way to do it. – G Josh Nov 23 '20 at 06:06

1 Answers1

0

To close the question, I went with Lodash's _.isEqual for comparison

if(!_.isEqual(array1, array2){
    callFunction1();
} else {
    null;
}
G Josh
  • 271
  • 1
  • 5
  • 15