Let's say I have two array of objects as,
let detail1 = [
{'book':'LOTR','price':'14'}
{'book':'Harry pottar','price':'12'},
]
let detail2 = [
{'book':'Harry pottar','price':'15'},
{'book':'LOTR','Price':'14'},
{'book':'HPP','Price':'21'}
]
I want to compare two array of objects and return true or false depending on whether they are same or not.
Note: object's structure i.e. key will always be same, their value may or may not change.
For this I tried as,
for(let i = 0 ;i<detail1.length; i++){
for(let j = 0; j<detail2.length; j++){
if(JSON.stringify(detail1[i]) === JSON.stringify(detail2[i])){
boolValue = true
} else {
boolValue = false
}
}
}
But this is giving incorrect value for let's say I have another array of object as,
let detail1 = [
{'book':'LOTR','price':'14'}
{'book':'Harry pottar','price':'12'},
]
let detail2 = [
{'book':'Harry pottar','price':'12'},
{'book':'LOTR','price':'14'}
]
But when order is changed it's giving incorrect value.
What is the possible solution to compare two array of object and return true if they are same and false if different for any cases given the structure of object i.e. key is same.