[javascript] Build tree array from flat array in javascript

_x000D_
_x000D_
var data = [{"country":"india","gender":"male","type":"lower","class":"X"},_x000D_
   {"country":"china","gender":"female","type":"upper"},_x000D_
   {"country":"india","gender":"female","type":"lower"},_x000D_
   {"country":"india","gender":"female","type":"upper"}];_x000D_
var seq = ["country","type","gender","class"];_x000D_
var treeData = createHieArr(data,seq);_x000D_
console.log(treeData)_x000D_
function createHieArr(data,seq){_x000D_
 var hieObj = createHieobj(data,seq,0),_x000D_
  hieArr = convertToHieArr(hieObj,"Top Level");_x000D_
  return [{"name": "Top Level", "parent": "null",_x000D_
         "children" : hieArr}]_x000D_
 function convertToHieArr(eachObj,parent){_x000D_
  var arr = [];_x000D_
  for(var i in eachObj){_x000D_
   arr.push({"name":i,"parent":parent,"children":convertToHieArr(eachObj[i],i)})_x000D_
  }_x000D_
  return arr;_x000D_
 }_x000D_
 function createHieobj(data,seq,ind){_x000D_
  var s = seq[ind];_x000D_
  if(s == undefined){_x000D_
   return [];_x000D_
  }_x000D_
  var childObj = {};_x000D_
  for(var ele of data){_x000D_
   if(ele[s] != undefined){_x000D_
    if(childObj[ele[s]] == undefined){_x000D_
     childObj[ele[s]] = [];_x000D_
    }_x000D_
    childObj[ele[s]].push(ele);_x000D_
   }_x000D_
  }_x000D_
  ind = ind+1;_x000D_
  for(var ch in childObj){_x000D_
   childObj[ch] = createHieobj(childObj[ch],seq,ind)_x000D_
  }_x000D_
  return childObj;_x000D_
 }_x000D_
}
_x000D_
_x000D_
_x000D_

Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to arrays

PHP array value passes to next row Use NSInteger as array index How do I show a message in the foreach loop? Objects are not valid as a React child. If you meant to render a collection of children, use an array instead Iterating over arrays in Python 3 Best way to "push" into C# array Sort Array of object by object field in Angular 6 Checking for duplicate strings in JavaScript array what does numpy ndarray shape do? How to round a numpy array?

Examples related to list

Convert List to Pandas Dataframe Column Python find elements in one list that are not in the other Sorting a list with stream.sorted() in Java Python Loop: List Index Out of Range How to combine two lists in R How do I multiply each element in a list by a number? Save a list to a .txt file The most efficient way to remove first N elements in a list? TypeError: list indices must be integers or slices, not str Parse JSON String into List<string>

Examples related to tree

Tree implementation in Java (root, parents and children) Build tree array from flat array in javascript Binary Search Tree - Java Implementation Difference between "Complete binary tree", "strict binary tree","full binary Tree"? Tree view of a directory/folder in Windows? Definition of a Balanced Tree Difference between binary tree and binary search tree How to create a collapsing tree table in html/css/js? How to search JSON tree with jQuery Non-recursive depth first search algorithm