Accepted answer

You just need to test if it's NOT an object. That works like a charm:

jsonData.forEach(function(link) {
        if (typeof(link.source) != "object"){
            link.source = nodeCircles[link.source] || (nodeCircles[link.source] = {name: link.sourceName, ID: link.source, class: link.sourceClass});
        if (typeof( != "object"){
   = nodeCircles[] || (nodeCircles[] = {name: link.targetName, ID:, class: link.targetClass});


I'm working on the same problem and got a "solution". It is still a prototype, but maybe it helps you.

Every mouse click on a node calls a function and detects new nodes. They are all saved in nodes and all active, it means all nodes on the display are in activeNodes. All nodes belonging to the path from the root node to the clicked one are stored in pathNodes.The result is, that only the active path is displayed inclusivly the children of the clicked one.

Hopefully it is clearly explained. For abetter understanding please look up the source code at:

For something to play with check out:

Here the snippet of my code:

 * Triggering mouse click start

function mousedown() {

  if (mousedown_node !== null) {

    var pathNodes = findPathNodesTo(mousedown_node);

    var point = d3.mouse(this),
      node = {
        id: nodes.length,
    node.x = point[0];
    node.y = point[1];

    var newNodes = findNodesbyParentId(,
      startingPoint = {
        x: mousedown_node.x,
        y: mousedown_node.y

    for (var i = 0; i < pathNodes.length; i++) {
      pathNodes[i].path = true;

    var removeNodes = activeNodes.diff(newNodes);
    var addNodes = newNodes.diff(pathNodes).diff(activeNodes);

    for (var i = 0; i < removeNodes.length; i++) {

    for (var i = 0; i < addNodes.length; i++) {
        addNodes[i].x = startingPoint.x;
        addNodes[i].y = startingPoint.y;
        activeLinks.push({source: findNode(addNodes[i].parent), target: findNode(addNodes[i].id)});

      // TODO: Find a smoother way do delay popping out new nodes



The last problem is to find a smooth way for popping out new nodes...

Related Query