Add new objects to JSON based on a selected object Javacript

Refresh

March 2019

Views

16 time

2

enter image description here So my requirement is if I click any node as shown in the picture, it should create child node which replicates its adjacent node(with its children) having new key, parent pair value as shown in the picture.

Say someone clicks node 0, it will create node 3 which replicate node 2 and its child and also will have new parent, key pair

I tried this in the link below but I am not able to achieve it

myDiagram.model.nodeDataArray.forEach(function(value, i) 
    if (value.level > thisemp.level) {
      var newemp = {};
      var level;
      switch (thisemp.level) {
        case 1:
          level = 1;
          break;
        case 2:
          level = 3;
          break;
        case 3:
          level = 5;
          break;
        case 4:
          level = 7;
          break;
        case 5:
          level = 9;
          break;
      }

      var parentValue = setCharAt(value.parent, level, z);
      var keyValue = setCharAt(value.key, level, z);
      newemp["key"] = keyValue;
      newemp["name"] = value.name;
      newemp["parent"] = parentValue;
      //newemp["level"]=value.level;

      data.push(newemp);
    }`enter code here

`

https://jsfiddle.net/dipunjan/c4boahx3/5/

0 answers