ocomfd
ocomfd

Reputation: 4020

Does calling JSON.stringify(obj) on 2 objects with same context always get the same string?

For example, I don't care the actual order of obj, I just want to know if object with same context would have same string result with stringify. My question is, suppose there are 2 different objects but have same key and value:

function check(v1, v2) {
  return JSON.stringify({
    a: v1,
    b: v2
  }) == JSON.stringify({
    a: 1,
    b: 2
  });
}

console.log(check(1, 2));

Does check(1,2) always return true?

Upvotes: 1

Views: 254

Answers (1)

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167192

Since you are hard-coding the location and values, this is reliable. And yes, no matter what, check(1,2) always returns true.

Just to add another note. If you wanna check multiple keys, this will not work. But in your case, this doesn't apply. So you can carry on. :)

Upvotes: 1

Related Questions