Avoid duplicate nodes in tree structure

Hi Walter,

I am creating a diagram using tree mapper sample where I have two trees. One is on the left side and another on the right side. My tree can have multiple levels (suppose 8 levels). So, I have to fetch the values of all the levels from the database and then draw the tree diagram. So, for this I have used nested loop of the levels means 8 levels. I am successfully able to create the tree but the problem which I am facing is the duplicate of nodes. So duplicate nodes are coming after the complete tree generation.

Please check the screen.

Here, you can see that 1.1.1 Recurring is repeating 3 times.

I have tried to check the duplicate values by using "diagram.findNodeForData(nodedata);" but its not able to find it values. I also tried diagram.model.containsNodeData(data) but again its not able to find the existence of duplicate value.

Can you please suggest how I can achieve the same?

Thank you.

The JavaScript Object that is in the Model.nodeDataArray is what is unique. So you can search for a node data in a Diagram by reference. You will never find any Node if you keep allocating new node data objects, unless you add those new node data objects to the model, in which case they will get new Nodes in the diagram – not any previously existing Node.

Every node data object will also have a unique key (string or number) as its “key” property value. The model gives it one if you haven’t already given it one. So you could look for it by key: Model.findNodeDataForKey or Diagram.findNodeForKey.

Hi Walter,

I tried the same. But it is giving me null value. Even the node exists.

Is there any other way to check the duplicate nodes. Please suggest.

Thank you.

Please don’t both private message us and post in the forum. Either send us email (not forum private message unless there is a problem with the forum) or post here, but always include screenshots and/or code describing what you have tried and how the results are different from what you want.

Sure, Walter. I will take care in future. Actually, I reply through my mobile so it goes into your private inbox.
I will use only forum.

Hi Walter,
I have already shared the screen shot. There, you can see that I am getting duplicate record of “1.1.1 Recurring”. “1.1.1 Recurring2”, “1.1.1 Recurring3”, “1.1.1 Recurring4” are all duplicate records which should not be in the tree.

Please check my JSON which I am using to create this tree.

{“CapabilityMap”:{“373”:{“cm_id”
:“373”,“cm_name”:“mapwithoutcap”,“Count”:8,“CapLevel”:“4”,“Level”:{“1”:{“Capability”:{“38537”:{“capability_id”
:“38537”,“caplevel”:“1”,“parentUniqueIndex”:“0”,“cm_id”:“373”,“name”:“Manufacturing and Operational Excellence”
,“unique_index”:“0_3”,“parent_id”:“0”,“cm_name”:“mapwithoutcap”,“importance_cap_organization”:null,“cap_assessment”
:null,“technology_health_check”:null,“business_health_check”:null,“AssocSpecs”:null}}},“2”:{“Capability”
:{“38538”:{“capability_id”:“38538”,“caplevel”:“2”,“parentUniqueIndex”:“0_3”,“cm_id”:“373”,“name”:“Production
Management”,“unique_index”:“0_3_1”,“parent_id”:“38537”,“cm_name”:“mapwithoutcap”,“importance_cap_organization”
:null,“cap_assessment”:null,“technology_health_check”:null,“business_health_check”:null,“AssocSpecs”
:“9,3”}}},“3”:{“Capability”:{“38539”:{“capability_id”:“38539”,“caplevel”:“3”,“parentUniqueIndex”:“0_3_1”
,“cm_id”:“373”,“name”:“Scheduling”,“unique_index”:“0_3_1_1”,“parent_id”:“38538”,“cm_name”:“mapwithoutcap”
,“importance_cap_organization”:null,“cap_assessment”:null,“technology_health_check”:null,“business_health_check”
:null,“AssocSpecs”:null},“38543”:{“capability_id”:“38543”,“caplevel”:“3”,“parentUniqueIndex”:“0_3_1”
,“cm_id”:“373”,“name”:“Manufacture to Sequence”,“unique_index”:“0_3_1_2”,“parent_id”:“38538”,“cm_name”
:“mapwithoutcap”,“importance_cap_organization”:null,“cap_assessment”:null,“technology_health_check”:null
,“business_health_check”:null,“AssocSpecs”:null},“38551”:{“capability_id”:“38551”,“caplevel”:“3”,“parentUniqueIndex”
:“0_3_1”,“cm_id”:“373”,“name”:“Batch Manufacturing”,“unique_index”:“0_3_1_3”,“parent_id”:“38538”,“cm_name”
:“mapwithoutcap”,“importance_cap_organization”:null,“cap_assessment”:null,“technology_health_check”:null
,“business_health_check”:null,“AssocSpecs”:null}}},“4”:{“Capability”:{“38541”:{“capability_id”:“38541”
,“caplevel”:“4”,“parentUniqueIndex”:“0_3_1_1”,“cm_id”:“373”,“name”:“Dispatching”,“unique_index”:“0_3_1_1_2”
,“parent_id”:“38539”,“cm_name”:“mapwithoutcap”,“importance_cap_organization”:null,“cap_assessment”:null
,“technology_health_check”:null,“business_health_check”:null,“AssocSpecs”:“9,9”},“38544”:{“capability_id”
:“38544”,“caplevel”:“4”,“parentUniqueIndex”:“0_3_1_2”,“cm_id”:“373”,“name”:“Components picking list”
,“unique_index”:“0_3_1_2_1”,“parent_id”:“38543”,“cm_name”:“mapwithoutcap”,“importance_cap_organization”
:null,“cap_assessment”:null,“technology_health_check”:null,“business_health_check”:null,“AssocSpecs”
:“9”},“38552”:{“capability_id”:“38552”,“caplevel”:“4”,“parentUniqueIndex”:“0_3_1_3”,“cm_id”:“373”,“name”
:“Components picking list”,“unique_index”:“0_3_1_3_1”,“parent_id”:“38551”,“cm_name”:“mapwithoutcap”,“importance_cap_organization”
:null,“cap_assessment”:null,“technology_health_check”:null,“business_health_check”:null,“AssocSpecs”
:“9”}}}}}}}

I have tried “diagram.findNodeForData(nodedata)” and also tried “diagram.model.containsNodeData(nodedata)” but I always gets the null value.

Hope it helps you to identify the problem.

Thank you.

You’re not showing me the code that you were trying that did not find what you were looking for.

Doesn’t my first reply explain why you are getting your current behavior?

Why haven’t you tried what I suggested in my first reply?

Hi Walter,

Yes, your reply explains it. I have a unique key of a node which is the combination of Id and Name. But, when I am trying to check the existence of node using either Diagram.findNodeForKey ot Model.findNodeDataForKey. It showing me the NULL value.

This is code which I have used

var capLevel3Key = translateUniqueIndex(capabilityUniqueIndex) + ’ ’ + capabilityName;
var capabilityData_Level3 = {key: capLevel3Key , group: leftKey};

var foundLvl3Key = myDiagram.findNodeForKey(capLevel3Key);

if(!foundLvl3Key) {
nodeDataArray.push(capabilityData_Level3);
linkDataArray.push({ from: capLevel2Key, to: capLevel3Key });
}

Here, I have used mydiagram.findNodeForKey but this is not considering that node exists or not and giving multiple nodes with the same name.

Please check the screen,

Here, I pasted only a code snippet. If you want the complete code file, then I will send you the complete file.

Thank you.

If you call Model.getKeyForNodeData of that “1.1.1 Recurring” Node.data, that will tell you the key that you should be looking for.

You can do it in the debugger by selecting that Node and evaluating myDiagram.model.getKeyForNodeData(myDiagram.selection.first().data)

Thanks Walter. I am able to find out the duplicate nodes by using this.