title: How does one convert a tree like nested data structure of arrays and objects into a list of items with computed/counted id's and tracked parent id's?
body: <p>An API returns a nested data structure of arrays and objects. The data comes as a list of tree like objects, each with a possible parent-child relationship. The structure itself is shown by the following example code.</p>
  <pre><code>[{
    label: &quot;search me&quot;,
    value: &quot;searchme&quot;,
    children: [{
  
      label: &quot;search me too&quot;,
      value: &quot;searchmetoo&quot;,
      children: [{
  
        label: &quot;No one can get me&quot;,
        value: &quot;anonymous&quot;,
      }],
    }],
  }, {
    label: &quot;search me2&quot;,
    value: &quot;searchme2&quot;,
    children: [{
  
      label: &quot;search me too2&quot;,
      value: &quot;searchmetoo2&quot;,
      children: [{
  
        label: &quot;No one can get me2&quot;,
        value: &quot;anonymous2&quot;,
      }],
    }],
  }]
  </code></pre>
  <p>The above data has to be converted into a (flat) array of objects where each object will represent a former node element but with a unique primary key (id). Also a node's parent-id equals its parent's id (the primary key) except for root nodes that do not have a parent, and therefore the parent-id then should be null.</p>
  <p>The target structure of the above provided source data then matches the following code ...</p>
  <pre><code>[{
    id: 1,                // DIAGID
    parentId: null,       // PARENTID
    label: &quot;search me&quot;,   // DIAGNOSIS
    value: &quot;searchme&quot;     // DIAGTYPE
  }, {
    id: 2,
    parentId: 1,
    label: &quot;search me too&quot;,
    value: &quot;searchmetoo&quot;
  }, {
    id: 3,
    parentId: 2,
    label: &quot;No one can get me&quot;,
    value: &quot;anonymous&quot;
  }, {
    id: 4,
    parentId: null,
    label: &quot;search me2&quot;,
    value: &quot;searchme2&quot;
  }, {
    id: 5,
    parentId: 4,
    label: &quot;search me too2&quot;,
    value: &quot;searchmetoo2&quot;
  }, {
    id: 6,
    parentId: 5,
    label: &quot;No one can get me2&quot;,
    value: &quot;anonymous2&quot;
  }]
  </code></pre>