The issue is that gData.nodes
is an array object - its keys are "0", "1", "2", not the id
property values of entries in the array.
A simple way of fixing this, short or restructuring the object's content, is to create a lookup table using the id value as the index key:
gData.index = {};
gData.nodes.forEach( node => gData.index[node.id] = node);
and then looking up the nodes from their id
where required:
const a = gData.index[link.source];
const b = gData.index[link.target];
Another solution might be to write a getNodeById
method for the gData
object.
Syntax errors generated by undefined values for a
and b
should resolve themselves once they are assigned correct node values. Obviously if error conditions are possible at run time, then error handling code should be included as well.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…