0

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.

Here is an output example: console log

I checked the Lodash documentation but I don't think there are comparison function for such case.

Community
  • 1
  • 1
xperator
  • 2,743
  • 7
  • 34
  • 58
  • 1
    https://stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript – suguspnk Jul 12 '17 at 08:08
  • Possible duplicate of [How to compare arrays in JavaScript?](https://stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript) – Yury Tarabanko Jul 12 '17 at 08:10
  • you can get the object properties using Object.values and then compare is that not possible ? – Rahul Singh Jul 12 '17 at 08:11
  • a simple equality test like this on js objects (and arrays) will only compare the vars reference value. you have to go deeper in properties if you want equality based on values – Kaddath Jul 12 '17 at 08:13
  • @Kaddath you mean I have to go through each one of the values and compare them one by one, like using a loop and such? – xperator Jul 12 '17 at 08:17
  • @YuryTarabanko Thanks for the link. I wish you could give some explanation though. So you mean this is all about arrays and I have to do an array comparison? I thought I could compare their data as a whole. Not go through the data one by one. – xperator Jul 12 '17 at 08:23
  • maybe this is inherited from the prototype nature, but basically yes, you have to choose yourself how deep you want to go, if order of properties is important, if constructors need to be the same for functions, etc. basically means writing your own function – Kaddath Jul 12 '17 at 08:24

1 Answers1

1

I've made a simple compare function in case that you have id prop. in your objects and want to check if there'are new rows in the array. Other way have to make a loop through all objects and check them one by one.

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
Yordan Nikolov
  • 2,598
  • 13
  • 16
  • Yeah they do have id prop. I forgot about that. Anyways I was thinking of using a single line equality test for a solution but I guess I have to compare the data itself and your answer looks like a great way to do it. – xperator Jul 12 '17 at 08:51
  • Alright so I don't know why but I couldn't get your piece of code working. It does indeed work for the examples you wrote but not with my data. Anyways since you guys led me to use array comparison functions, I found the `_.differenceBy` function in Lodash docs which works kinda same way your code does, it takes 2 arrays as the first argument and an `id` for the second argument then it outputs the differences the arrays have based on the `id`. – xperator Jul 12 '17 at 13:48
  • I cant help you more, because I can't see your code. As long as you compare the ids from the arrays, like I suggest you, there's no reason not work. That's what I can help you with for now. – Yordan Nikolov Jul 12 '17 at 13:54
  • It's ok as I said I'm using the Lodash function I mentioned to do the exact same thing so it's working perfectly. Just the idea of using `id` prop and showing an example was enough and a great help. Thx! – xperator Jul 12 '17 at 18:18