Build tree array from flat array in javascript
I have a complex json file that I have to handle with javascript to make it hierarchical, in order to later build a tree.Every entry of the json has :id : a unique id, parentId : the id of the parent...
View ArticleAnswer by Halcyon for Build tree array from flat array in javascript
There is an efficient solution if you use a map-lookup. If the parents always come before their children you can merge the two for-loops. It supports multiple roots. It gives an error on dangling...
View ArticleAnswer by Stephen Harris for Build tree array from flat array in javascript
As mentioned by @Sander, @Halcyon`s answer assumes a pre-sorted array, the following does not. (It does however assume you have loaded underscore.js - though it could be written in vanilla...
View ArticleAnswer by alexandru.pausan for Build tree array from flat array in javascript
Had the same problem, but I could not be certain that the data was sorted or not. I could not use a 3rd party library so this is just vanilla Js; Input data can be taken from @Stephen's example; var...
View ArticleAnswer by DenQ for Build tree array from flat array in javascript
It may be useful package list-to-treeInstall: bower install list-to-tree --saveornpm install list-to-tree --saveFor example, have list:var list = [ { id: 1, parent: 0 }, { id: 2, parent: 1 }, { id: 3,...
View ArticleAnswer by William Leung for Build tree array from flat array in javascript
a more simple function list-to-tree-litenpm install list-to-tree-litelistToTree(list)source: function listToTree(data, options) { options = options || {}; var ID_KEY = options.idKey || 'id'; var...
View ArticleAnswer by Eric D. Fields for Build tree array from flat array in javascript
Here's a simple helper function that I created modeled after the above answers, tailored to a Babel environment:import { isEmpty } from 'lodash'export default function unflattenEntities(entities,...
View ArticleAnswer by walter ye for Build tree array from flat array in javascript
also do it with lodashjs(v4.x)function buildTree(arr){ var a=_.keyBy(arr, 'id') return _ .chain(arr) .groupBy('parentId') .forEach(function(v,k){ k!='0'&&...
View ArticleAnswer by nehresman for Build tree array from flat array in javascript
Here is a modified version of Steven Harris' that is plain ES5 and returns an object keyed on the id rather than returning an array of nodes at both the top level and for the children.unflattenToObject...
View ArticleAnswer by FurkanO for Build tree array from flat array in javascript
( BONUS1 : NODES MAY or MAY NOT BE ORDERED )( BONUS2 : NO 3RD PARTY LIBRARY NEEDED, PLAIN JS )( BONUS3 : User "Elias Rabl" says this is the most performant solution, see his answer below )Here it...
View ArticleAnswer by Nina Scholz for Build tree array from flat array in javascript
UPDATE 2022This is a proposal for unordered items. This function works with a single loop and with a hash table and collects all items with their id. If a root node is found, then the object is added...
View ArticleAnswer by Nick Licata for Build tree array from flat array in javascript
This is a modified version of the above that works with multiple root items, I use GUIDs for my ids and parentIds so in the UI that creates them I hard code root items to something like...
View ArticleAnswer by Hex.style for Build tree array from flat array in javascript
I like @WilliamLeung's pure JavaScript solution, but sometimes you need to make changes in existing array to keep a reference to object.function listToTree(data, options) { options = options || {}; var...
View ArticleAnswer by Iman Bahrampour for Build tree array from flat array in javascript
You can handle this question with just two line coding:flatArray.forEach(f=> {f.nodes=flatArray.filter(g=>g.parentId===f.id)});var resultArray=flatArray.filter(f=>f.parentId==null);var...
View ArticleAnswer by stywell for Build tree array from flat array in javascript
Copied from the Internethttp://jsfiddle.net/stywell/k9x2a3g6/ function list2tree(data, opt) { opt = opt || {}; var KEY_ID = opt.key_id || 'ID'; var KEY_PARENT = opt.key_parent || 'FatherID'; var...
View ArticleAnswer by ДенищукПавлоМиколайович for Build tree array from flat array in...
You can use npm package array-to-tree https://github.com/alferov/array-to-tree.It's convert a plain array of nodes (with pointers to parent nodes) to a nested data structure.Solves a problem with...
View ArticleAnswer by unplugged for Build tree array from flat array in javascript
without third party libraryno need for pre-ordering arrayyou can get any portion of the tree you wantTry thisfunction getUnflatten(arr,parentid){ let output = [] for(const obj of arr){ if(obj.parentid...
View ArticleAnswer by josesuero for Build tree array from flat array in javascript
This is an old thread but I figured an update never hurts, with ES6 you can do:const data = [{ id: 1, parent_id: 0}, { id: 2, parent_id: 1}, { id: 3, parent_id: 1}, { id: 4, parent_id: 2}, { id: 5,...
View ArticleAnswer by shekhardtu for Build tree array from flat array in javascript
Use this ES6 approach. Works like charm // Data Set// One top level comment const comments = [{ id: 1, parent_id: null}, { id: 2, parent_id: 1}, { id: 3, parent_id: 1}, { id: 4, parent_id: 2}, { id: 5,...
View Article