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