Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to build tree array from flat array in javascript

function list_to_tree(list) {
  var map = {}, node, roots = [], i;
  
  for (i = 0; i < list.length; i += 1) {
    map[list[i].id] = i; // initialize the map
    list[i].children = []; // initialize the children
  }
  
  for (i = 0; i < list.length; i += 1) {
    node = list[i];
    if (node.parentId !== "0") {
      // if you have dangling branches check that map[node.parentId] exists
      list[map[node.parentId]].children.push(node);
    } else {
      roots.push(node);
    }
  }
  return roots;
}

var entries = [{
    "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
  }
];

console.log(list_to_tree(entries));
Comment

how to build tree array from flat array in javascript

const arr = [
   {
      "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
   }
];
Comment

PREVIOUS NEXT
Code Example
Javascript :: buttons js before submit 
Javascript :: como ordenar um array em ordem crescente javascript 
Javascript :: insertar al inicio de un array javascript 
Javascript :: angular tooltip text ngif 
Javascript :: how to add data modal target attribute in jquery 
Javascript :: set methods in js 
Javascript :: how-to-close-current-tab-in-a-browser-window 
Javascript :: JSON parse error: Cannot deserialize value of type `java.util.Date` from String 
Javascript :: checks for valid email address syntax javascript 
Javascript :: javascript string mutable 
Javascript :: Supported by YAML but not supported by JSON: 
Javascript :: javascript map callback function 
Javascript :: define conastant js 
Javascript :: javascript startdate end date 
Javascript :: jquery view image in codeigniter 
Javascript :: import json file in the same directory as javascript 
Javascript :: convert arrow function to normal function javascript online 
Python :: python most used functions 
Python :: display all columns in pandas 
Python :: how to make a resizable pygame window 
Python :: simple flask hello world 
Python :: spinning donut python 
Python :: json list to dataframe python 
Python :: plotly not showing in jupyter 
Python :: enumerate zip python 
Python :: add bearer token in python request 
Python :: how to print a list without brackets and commas python 
Python :: pyspark convert float results to integer replace 
Python :: drop unnamed column pandas 
Python :: install python-dev packages 
ADD CONTENT
Topic
Content
Source link
Name
6+4 =