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.

7
  • 1
    stackoverflow.com/questions/7837456/… Commented Jul 12, 2017 at 8:08
  • Possible duplicate of How to compare arrays in JavaScript? Commented Jul 12, 2017 at 8:10
  • you can get the object properties using Object.values and then compare is that not possible ? Commented Jul 12, 2017 at 8: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 Commented Jul 12, 2017 at 8: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? Commented Jul 12, 2017 at 8:17

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

4 Comments

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.
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.
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.
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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.