Home > Blockchain >  Recursive Function in TypeScript - Parents Array
Recursive Function in TypeScript - Parents Array

Time:12-21

I want to create a recursive function that receive a List of objects that contains the id and parent_id. If the parent of an element is in the list I want to remove it and add it to the parent.

Convert this:

{
  "id": 180,
  "children": [],
  "parent_id": 195,
  "name": "Object 180"
},
{
  "id": 193,
  "children": [],
  "parent_id": 180,
  "name": "Object 193"
},
{
  "id": 194,
  "children": [],
  "parent_id": 180,
  "name": "Object 194"
}
{
  "id": 199,
  "children": [],
  "parent_id": 187,
  "name": "Object 199"
}
{
  "id": 304,
  "children": [],
  "parent_id": 193,
  "name": "Object 304"
}

To this:

{
  "id": 180,
  "children": [
    {
      "id": 193,
      "children": [
        {
          "id": 304,
          "children": [],
          "parent_id": 193,
          "name": "Object 304"
         }
      ],
      "parent_id": 180,
      "name": "Object 193"
    },
    {
      "id": 194,
      "children": [],
      "parent_id": 180,
      "name": "Object 194"
    }
  ],
  "parent_id": 195,
  "name": "Object 180"
},
{
  "id": 199,
  "children": [],
  "parent_id": 187,
  "name": "Object 199"
}

Sometimes the parent_id is null, and there is no limit of levels of the parents.

CodePudding user response:

You don't need a recursive function. Just track items you've already seen and if parent exists in it add to parent.children or add a new root node.

An example complete solution is attached.

Complete code

type Item = {
    id: number,
    children: Item[],
    parent_id: number,
    name: string,
}

const items: Item[] = [
    {
        "id": 180,
        "children": [],
        "parent_id": 195,
        "name": "Object 180"
    },
    {
        "id": 193,
        "children": [],
        "parent_id": 180,
        "name": "Object 193"
    },
    {
        "id": 194,
        "children": [],
        "parent_id": 180,
        "name": "Object 194"
    },
    {
        "id": 199,
        "children": [],
        "parent_id": 187,
        "name": "Object 199"
    },
    {
        "id": 304,
        "children": [],
        "parent_id": 193,
        "name": "Object 304"
    }
];


function nest(items:Item[]): Item[] {
  const output: Item[] = [];
  const idToItem = new Map<number,Item>();
  for (let item of items) {
      // Either add to parent. Or create a new root level node
      if (idToItem.has(item.parent_id)) {
          idToItem.get(item.parent_id)!.children.push(item);
      } else {
          idToItem.set(item.id, item);
          output.push(item);
      }
  }
  return output;
}

console.log(nest(items));

CodePudding user response:

Since basarat's answer does not account for items nested more than one level.

Here a solution that creates an output with arbitrary nesting-depth:

const listToTree = (input) => {
  const map = new Map(input.map((item) => [item.id, item]));
  
  const output = [];
  for (const item of input) {
    if (map.has(item.parent_id)) {
      map.get(item.parent_id).children.push(map.get(item.id));
    } else {
      output.push(map.get(item.id));
    }
  }
  return output;
};

const input = [
  {
    "id": 180,
    "children": [],
    "parent_id": 195,
    "name": "Object 180"
  },
  {
    "id": 193,
    "children": [],
    "parent_id": 180,
    "name": "Object 193"
  },
  {
    "id": 194,
    "children": [],
    "parent_id": 180,
    "name": "Object 194"
  },
  {
    "id": 199,
    "children": [],
    "parent_id": 187,
    "name": "Object 199"
  },
  {
    "id": 304,
    "children": [],
    "parent_id": 193,
    "name": "Object 304"
  },
  {
    "id": 305,
    "children": [],
    "parent_id": 194,
    "name": "Object 304"
  }
];

const output = listToTree(input);

console.log(output);

  • Related