0
var arr = [
  {
     groups: {
       TypeID: 'AV601'
     }
  }
]

var prop = {
  TypeID: 'AV601'
}

console.log(arr[0].groups);
console.log(prop);
console.log(arr[0].groups === prop)// false

How can this give false? They are the same?

https://jsbin.com/giholunupa/edit?js,console

Joe
  • 4,274
  • 32
  • 95
  • 175
  • possible duplicate of [Why are two identical objects not equal to each other](http://stackoverflow.com/questions/11704971/why-are-two-identical-objects-not-equal-to-each-other). – ryeballar Feb 18 '17 at 14:00

1 Answers1

1

It's because Javascript compares objects by reference, not by keys equality. Those two objects simply have the same key name with the equal key value, but in memory, they point to different address.

let o1= {}; // points to address "#aaa" in memory
let o2= {}; // points to address "#bbb" in memory

o1 === o2; // false
Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488
  • Ok. So I have to compare each prop? – Joe Feb 18 '17 at 14:01
  • 1
    It is better to refer to the Q&A of which this is a duplicate – trincot Feb 18 '17 at 14:01
  • @PerStröm, it depends on what is your task. If you want to know whether objects have the same key/value pairs, yes, you have to compare by keys. If you want to know if its the same object, you have to compare by reference. – Max Koretskyi Feb 18 '17 at 14:03