Skip to content

Array to Tree

Converts a flat node array into a tree structure.

Parameters

It takes two parameters:

  • Array: Flat node array

  • Options: An optional parameter object used to configure the specific behavior of the conversion method

    ParameterDescriptionTypeDefault
    childrenKeyCustom node children field namestring'children'
    idKeyCustom node ID field namestring'id'
    pidKeyCustom node parent ID field namestring'pid'

Example

javascript
const flatArray = [
  { uid: '1', name: 'node1', pid: null },
  { uid: '2', name: 'node2', pid: '1' },
  { uid: '3', name: 'node3', pid: '1' },
  { uid: '4', name: 'node4', pid: '2' },
  { uid: '5', name: 'node5', pid: '2' },
  { uid: '6', name: 'node6', pid: '3' }
];

const options = {
  idKey: 'id',
  pidKey: 'pid',
  childrenKey: 'children'
};

const treeArray = arrayToTree(flatArray, options);

Released under the MIT License.