title: Modifying tree structured data in JavaScript with given condition?
body: <p>I have a tree data like this:</p>
  
  <pre><code>var tree = [
    {
      id:"11",
      text: "Parent 1",
      type:"Parent",
      nodeSelected:false,
      nodes: [
        {
          text: "Child 1",
          parentId: "11",
          id:"21",
          nodeSelected:false,
          type: "Child",
          nodes: [
            {
              id:"36",
              text: "Grandchild 1",
              parentId: "21",
              nodeSelected:false,
              nodes:[],
              type: "Grandchild"
            },
            {
              id:"38",
              text: "Grandchild 2",
              parentId: "21",
              nodes:[],
              nodeSelected:false,
              type: "Grandchild"
            }
          ]
        },
        {
          id:"43",
          text: "Child 2",
          nodeSelected:false,
          parentId:"11",
          type: "Child",
          nodes: [
              {
              id:"52",
              parentId:"43",
              text: "Grandchild 1 of child 2",
              nodeSelected:false,
              nodes:[],
              type: "Grandchild"
            }
          ]
        }
      ]
    },
    {
      id:"46",
      text: "Parent 2",
      nodeSelected:false,
      type: "Parent",
      nodes:[]
    },
    {
      id:"48",
      text: "Parent 3",
      nodeSelected:false,
      type: "Parent",
      node:  [
          {
              id:"70",
              text: "child 3",
              parentId: "48",
              type: "Child",
              nodeSelected:false,
              nodes:[]
          }
      ]
    }
  ];
  </code></pre>
  
  <p>All of the <code>nodeSelected</code> are false. </p>
  
  <p>I have group of Ids in form of an array. </p>
  
  <pre><code>groupedIds=["11","21","43","52","48"];
  </code></pre>
  
  <p>I want to do <code>nodeSelected</code> property true based on <code>groupId array</code> with some condition.</p>
  
  <p>the condition is such that if a parentId is mentioned along with its children ids then <code>nodeSelected</code> property of parent should remain
  false and child's <code>nodeSelected</code> should be true (and its <code>nodes</code> <code>nodeSelected</code> should also be 'true').
  Or else whole parent nodes <code>nodeSelected</code> should be true(along with it nodes).</p>
  
  <p>So the result will be like this:</p>
  
  <pre><code>var resultArray = [
    {
      id:"11",
      text: "Parent 1",
      type:"Parent",
      nodeSelected:false,
      nodes: [
        {
          text: "Child 1",
          parentId: "11",
          id:"21",
          nodeSelected:true,
          type: "Child",
          nodes: [
            {
              id:"36",
              text: "Grandchild 1",
              parentId: "21",
              nodeSelected:true,  
              type: "Grandchild"
            },
            {
              id:"38",
              text: "Grandchild 2",
              parentId: "21",
              nodeSelected:true,
              type: "Grandchild"
            }
          ]
        },
        {
          id:"43",
          text: "Child 2",
          nodeSelected:false,
          parentId:"11",
          type: "Child",
          nodes: [
              {
              id:"52",
              parentId:"43",
              text: "Grandchild 1 of child 2",
              nodeSelected:true,
              type: "Grandchild"
            }
          ]
        }
      ]
    },
    {
      id:"46",
      text: "Parent 2",
      nodeSelected:false,
      type: "Parent"
    },
    {
      id:"48",
      text: "Parent 3",
      nodeSelected:true,
      type: "Parent",
      node:  [
          {
              id:"70",
              text: "child 3",
              parentId: "48",
              type: "Child",
              nodeSelected:true
          }
      ]
    }
  ];
  </code></pre>
  
  <p>(<strong>My try</strong>) Although incomplete but something like this we can do</p>
  
  <pre><code>tree.forEach((Parent) =&gt; {
              if (groupedIds.includes(Parent.id)) {
                  let isSomeChildSelected = Parent.nodes.some((loc) =&gt; groupedIds.includes(loc.id));
                  if (isSomeChildSelected) {
                      Parent.nodes.forEach((child) =&gt; {
                          if (groupedIds.includes(child.id)) {
                              let isSomeGrandChildSelected = child.nodes.some((grandchild) =&gt; groupedIds.includes(grandchild.id));
                              if (isSomeGrandChildSelected) {
                                  child.nodes.forEach((grandchild) =&gt; {
                                      if (groupedIds.includes(grandchild.id)) {
                                          grandchild.isSelected = true;
                                      }
                                  })
                              } else {
                                  child.isSelected = true;
                                  child.nodes.forEach((grandchild) =&gt; {
                                      grandchild.isSelected = true;
                                  })
                              }
                          }
                      })
                  } else {
                      Parent.isSelected = true;
                      Parent.nodes.forEach((child) =&gt; {
                          child.isSelected = true;
                          child.nodes.forEach((grandchild) =&gt; {
                              grandchild.isSelected = true;
                          })
                      })
                  }
              }
          })
  </code></pre>
  
  <p>The above tried method solves the issue to some extent, but it is a bit complex.</p>
  
  <p>Any help would be much appreciated. Thanks!</p>