function treeToArray(nodes) {
  const a = [];
  function dfs(nodes, depth, offset, path = []) {
    if (nodes.length === 0) {
      return { span: 1, maxDepth: 0 };
    }
    let totalSpan = 0;
    let maxDepth = 0;
    for (const {
      children,
      rawValue,
      isGrandTotal,
      isValueColumn,
      ...rest
    } of nodes) {
      const pathWithValue =
        isValueColumn || isGrandTotal ? null : [...path, rawValue];
      const item = {
        ...rest,
        rawValue,
        isGrandTotal,
        depth,
        offset,
        hasChildren: children.length > 0,
        path: pathWithValue,
      };
      a.push(item);
      const result = dfs(children, depth + 1, offset, pathWithValue);
      item.span = result.span;
      item.maxDepthBelow = result.maxDepth;
      offset += result.span;
      totalSpan += result.span;
      maxDepth = Math.max(maxDepth, result.maxDepth);
    }
    return { span: totalSpan, maxDepth: maxDepth + 1 };
  }

  dfs(nodes, 0, 0);
  return a;
}
// We track the depth, offset, etc to know how to line items up in the headers.
We track the depth, offset, etc to know how to line items up in the headers.
We track the depth offset etc to know how to line items up in the headers .
function treeToArray(nodes) {
  const a = [];
  function dfs(nodes, depth, offset, path = []) {
    if (nodes.length === 0) {
      return { span: 1, maxDepth: 0 };
    }
    let totalSpan = 0;
    let maxDepth = 0;
    for (const {
      children,
      rawValue,
      isGrandTotal,
      isValueColumn,
      ...rest
    } of nodes) {
      const pathWithValue =
        isValueColumn || isGrandTotal ? null : [...path, rawValue];
      const item = {
        ...rest,
        rawValue,
        isGrandTotal,
        depth,
        offset,
        hasChildren: children.length > 0,
        path: pathWithValue,
      };
      a.push(item);
      const result = dfs(children, depth + 1, offset, pathWithValue);
      item.span = result.span;
      item.maxDepthBelow = result.maxDepth;
      offset += result.span;
      totalSpan += result.span;
      maxDepth = Math.max(maxDepth, result.maxDepth);
    }
    return { span: totalSpan, maxDepth: maxDepth + 1 };
  }
  dfs(nodes, 0, 0);
  return a;
}
function treeToArray ( nodes ) { const a = [ ] ; function dfs ( nodes , depth , offset , path = [ ] ) { if ( nodes . length === 0 ) { return { span : 1 , maxDepth : 0 } ; } let totalSpan = 0 ; let maxDepth = 0 ; for ( const { children , rawValue , isGrandTotal , isValueColumn , ... rest } of nodes ) { const pathWithValue = isValueColumn || isGrandTotal ? null : [ ... path , rawValue ] ; const item = { ... rest , rawValue , isGrandTotal , depth , offset , hasChildren : children . length > 0 , path : pathWithValue , } ; a . push ( item ) ; const result = dfs ( children , depth + 1 , offset , pathWithValue ) ; item . span = result . span ; item . maxDepthBelow = result . maxDepth ; offset += result . span ; totalSpan += result . span ; maxDepth = Math . max ( maxDepth , result . maxDepth ) ; } return { span : totalSpan , maxDepth : maxDepth + 1 } ; } dfs ( nodes , 0 , 0 ) ; return a ; }
We track the depth, offset, etc to know how to line items up in the headers.
We track the depth offset etc to know how to line items up in the headers .