Q:

How to create a tree-like structure from a JavaScript Object

This is the Object sample I'm given:
Input :

{
      "People": [
          {
              "id": "12",
              "parentId": "0",
              "text": "Man",
              "level": "1",
              "children": null
          },
          {
              "id": "6",
              "parentId": "12",
              "text": "Boy",
              "level": "2",
              "children": null
          },
                  {
              "id": "7",
              "parentId": "12",
              "text": "Other",
              "level": "2",
              "children": null
          },
          {
              "id": "9",
              "parentId": "0",
              "text": "Woman",
              "level": "1",
              "children": null
          },
          {
              "id": "11",
              "parentId": "9",
              "text": "Girl",
              "level": "2",
              "children": null
          }
      ] }

I want to transform it to a JSON format like this:  
{
    "People": [
        {
            "id": "12",
            "parentId": "0",
            "text": "Man",
            "level": "1",
            "children": [
                {
                    "id": "6",
                    "parentId": "12",
                    "text": "Boy",
                    "level": "2",
                    "children": null
                },
                {
                    "id": "7",
                    "parentId": "12",
                    "text": "Other",
                    "level": "2",
                    "children": null
                }   
            ]
        }
}

Any Ideas/Help would be appreciated

A:

You could use an object for the reference to either child or parent and collect the children and parents to get only the person for the root.

var data = { John: "James", Samar: "Michel", Albert: "Michel", Michel: "James", James: "Sarah" },
    parents = new Set,
    children = new Set,
    references = {},
    result;

Object
    .entries(data)
    .forEach(([child, parent]) => {
        references[child] = references[child] || [];
        references[parent] = references[parent] || [];
        references[parent].push({ [child]: references[child] });
        parents.add(parent);
        children.add(child);
    });

result = [...parents]
    .filter(p => !children.has(p))
    .map(p => ({ [p]: references[p] }));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

