Can't access object property, even though it shows up in a console log

后端 未结 30 1800
日久生厌
日久生厌 2020-11-22 06:26

Below, you can see the output from these two logs. The first clearly shows the full object with the property I\'m trying to access, but on the very next line of code, I can\

30条回答
  •  鱼传尺愫
    2020-11-22 07:07

    Just in case this is helpful for someone, I had a similar problem, and it's because someone created an override for .toJSON in the object I was working with. So the object was something like:

    {
      foo: {
             bar: "Hello"
             baz: "World"
           }
    }
    

    But .toJSON() was:

    toJSON() {
      return this.foo
    }
    

    So when I called JSON.stringify(myObject) it returned "{"bar": "Hello", "baz": "World"}". However, Object.keys(myObject) revealed the "foo".

提交回复
热议问题