0

I am currently implementing a modified version of this D3 collapsible tree. When I am creating the links, there are two key-value pairs (x0 and y0) that appear when I console log a certain object, but that are undefined when logged individually. They also are not listed in the object's keys when using the Object.keys() method. See below:

enter image description here

How do I access the missing values?

223seneca
  • 1,136
  • 3
  • 19
  • 47
  • 2
    Are they dynamically added later? The console is lazy loading so it is not a snapshot that that moment in time. `console.log(JSON.stringify(d.source))` – epascarello Oct 11 '19 at 15:35
  • I'm not sure what you mean. These console logs are on consecutive lines of synchronous code, so I don't think the object could have been modified in the interim. `console.log(JSON.stringify(d.source))` does not work. I receive `TypeError: Converting circular structure to JSON`. – 223seneca Oct 11 '19 at 15:36
  • can you try log `d.source` in the end again? – apple apple Oct 11 '19 at 15:38
  • and please explain why you need access to these properties. – apple apple Oct 11 '19 at 15:38
  • 1
    @223seneca [see here](https://stackoverflow.com/questions/8249136/why-does-javascript-object-show-different-values-in-console-in-chrome-firefox) you don't need async code to trigger this behaviour: `obj = {a: 1}; console.log(obj); obj.a = 2;` will do it. You print `obj` but it's not a snapshot of what `obj` was at the time of printing it - it's printed as a "live object" (or the reference evaluated later). So, when you expand this, you'll see `a: 2` even though it was `a: 1` at the time of printing. – VLAZ Oct 11 '19 at 15:40
  • When I log `d.source` afterwards, the missing keys appear again. I need these properties in order to make the line transitions render properly. – 223seneca Oct 11 '19 at 15:40
  • and `Object.keys()` only returns enumerable property names (although it seems not the case) – apple apple Oct 11 '19 at 15:40
  • These are enumerable property names, are they not? – 223seneca Oct 11 '19 at 15:43
  • 1
    They are enumerable but if you *don't* see them from `Object.keys` yet you see them when the object is printed, it suggest the issue is what @epascarello mentioned - the properties are added later. `Object.keys` *would* give you an accurate snapshot of the keys present at that point in time, printing the object doesn't. – VLAZ Oct 11 '19 at 15:45
  • Ok, that makes sense. I have no idea where these could be added later, though. It may be some nuance of D3. – 223seneca Oct 11 '19 at 15:48
  • `var a = {}; console.log(a); a.foo = '1';` inspect the object in the console. – epascarello Oct 11 '19 at 16:00
  • Yes, I can replicate the behavior you're describing. – 223seneca Oct 11 '19 at 16:03

0 Answers0