2

I have a graph with nodes that carry more attributes than names/ids, but also the degree for some filtering. When I run some logging, for some reason d3 mistakes the object (node) for a string.

The code:

svg.selectAll("line").each(function (l) {
    let opacity =  l.source.degree >= nodeDegreeFilter && l.target.degree >= nodeDegreeFilter ? 1 : 0;
    d3.select(this).style("opacity", opacity);
    console.info(opacity, l.target, typeof l.target, l);
  });

yields this output:

0 North Carolina string Object
{
​
index: 222
​
source: Object { id: "Impotency", group: 2, class: 1, … }
​
target: Object { id: "North Carolina", group: 2, class: 0, … }
​
value: 1
}

Can someone help me figure out what is going on?

mimre
  • 92
  • 1
  • 9
  • 1
    I cannot reproduce this (the close vote is mine). `l.target` should be `{ id: "North Carolina", group: 2, class: 0, … }`, not `North Carolina`. The best idea for getting proper help is creating a [MCVE] reproducing the issue. – Gerardo Furtado Sep 15 '19 at 12:40
  • Thank you for your suggestion. I generally try to do so, however it's not always possible. However, I found out, that the bug is not cause by an incorrect type coercion/conversion, but rather that console.log does not report the correct values. I cannot tell why it does that tho. – mimre Sep 16 '19 at 14:15
  • 2
    according to [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/info), `console.info` (and `console.log`) can use the string representation of the passed arguments. It looks like the `l` object is using its `id` property for its string representation. You can use [`console.dir`](https://developer.mozilla.org/en-US/docs/Web/API/Console/dir) to output the object representation (though it takes only one argument at a time). For more info see: https://stackoverflow.com/questions/36215379/does-console-log-invokes-tostring-method-of-an-object – Steve Sep 16 '19 at 18:13

0 Answers0