I'm simply trying to compare the notifications list received from the server to the one I've stored on the local side.
if(myApp.NewNotifications != response.data['unread_notifications']){
return true;
} else { return false; }
The problem is that even when they both have the same data, it returns false.
Since I'm using Vue.js on the client side, I assume it attaches some properties to the local object which makes it different to the one received from the backend.
I checked the Lodash documentation but I don't think there are comparison function for such case.
答案 0 :(得分:1)
如果你有id prop,我已经做了一个简单的比较功能。在您的对象中,并想要检查数组中是否有新行。其他方法必须循环遍历所有对象并逐个检查它们。
function simpleComparision(oldVal, newVal) {
if(oldVal.length !== newVal.length) {
return false;
}
return newVal.filter(function(a) {
return oldVal.map(function(b) { return b.id; }).indexOf(a.id)===-1;
}).length === 0 ? true:false;
}
var equal = simpleComparision([{id: 1}, {id: 2}], [{id: 2}, {id: 1}]);
console.log(equal); // they are equal
equal = simpleComparision([{id: 1}, {id: 2}], [{id: 2}, {id: 3}]);
console.log(equal); // they are not equal
equal = simpleComparision([{id: 1}, {id: 2}], [{id: 2}, {id: 1}, {id: 3}]);
console.log(equal); // they are not equal