1

I have an object with multiple properties, one of which is 'lane' with a value of 1. console.log(myObject), shows the property and its value are there. However, if I try to access it myObject.lane, it is'undefined'. Also - if I print the keys of the object with Object.keys(myObject), the array has all of the properties EXCEPT "lane"! I really don't know what else to do, or what kind of problem could cause this.

//actual code (in a React Redux reducer... which shouldn't matter in my opinion)
      console.group("What is happening?");
      console.log("myObject", myObject);
      console.log(
        "you can clearly see that 'lane' in is myObject with value of number 1."
      );
      console.log("Object.keys(myObject)", Object.keys(myObject));
      console.log("But somehow, 'lane' is not a key in myObject");
      console.log("MyObject again", myObject);
      console.log("myObject.lane is undefined? : ", myObject.lane);
      console.log("Other keys work, like myObject.alpha : ", myObject.alpha);
      console.groupEnd();

Here is a picture of the output of the console.

enter image description here

What can cause an object to console.log a property and its value, but not actually be a key?

NicoWheat
  • 2,157
  • 2
  • 26
  • 34
  • you should share how ***you*** create the object, especially the `lane` – apple apple Mar 22 '20 at 04:01
  • 1
    I can *guess* it's because `lane` is actually not exist at that moment. Note that `console.log` does not create a deep copy of the object. – apple apple Mar 22 '20 at 04:05
  • 2
    The console doesn't print a snapshot of the value you're logging, but rather it prints a reference to the object. To print the current value see what `console.log(JSON.stringify(myObject))` gives – Nick Parsons Mar 22 '20 at 04:07
  • ..would you think it doesn't exist in that moment? but still shows up right before and right after with console.log(myObject)? – NicoWheat Mar 22 '20 at 04:09
  • console.log(JSON.stringify(myObject)) shows that the property is actually not there. Thanks! – NicoWheat Mar 22 '20 at 04:25
  • @NicoWheat no worries, you can see this question for more details: [Weird behavior with objects & console.log](https://stackoverflow.com/q/23429203) about console.log – Nick Parsons Mar 22 '20 at 04:26

0 Answers0