Quantcast
Channel: Build tree array from flat array in javascript - Stack Overflow
Browsing all 35 articles
Browse latest View live

Answer by Amir for Build tree array from flat array in javascript

I used @FurkanO answer and made a generic function that can be used with any object type, I also wrote this function in TypeScript which i love it more because of auto completions.Implementation:1....

View Article



Answer by Daniel Bellmas for Build tree array from flat array in javascript

After many tries I came up with this:const arrayToTree = (arr, parent = 0) => arr .filter(item => item.parent === parent).map(child => ({ ...child, children: arrayToTree(arr, child.index)...

View Article

Answer by Danil Apsadikov for Build tree array from flat array in javascript

Array elements can be in a chaotic orderlet array = [ { id: 1, data: 'something', parent_id: null, children: [] }, { id: 2, data: 'something', parent_id: 1, children: [] }, { id: 5, data: 'something',...

View Article

Answer by natrayan for Build tree array from flat array in javascript

Incase anyone needs it for multiple parent. Refer id 2 which has multiple parents const dataSet = [{"ID": 1, "Phone": "(403) 125-2552","City": "Coevorden","Name": "Grady" }, {"ID": 2,"Phone": "(403)...

View Article

Answer by Ali Zemani for Build tree array from flat array in javascript

ES6 Map version :getTreeData = (items) => { if (items && items.length > 0) { const data = []; const map = {}; items.map((item) => { const id = item.id; // custom id selector !!! if...

View Article


Answer by Arik for Build tree array from flat array in javascript

My solution:Allows bi-directional mapping (root to leaves and leaves to root)Returns all nodes, roots, and leavesOne data pass and very fast performanceVanilla Javascript/** * * @param data items array...

View Article

Answer by pekaaw for Build tree array from flat array in javascript

Based on @FurkanO's answer, I created another version that does not mutate the origial data (like @Dac0d3r requested). I really liked @shekhardtu's answer, but realized it had to filter through the...

View Article

Image may be NSFW.
Clik here to view.

Answer by Yusufbek for Build tree array from flat array in javascript

Answer to a similar question:https://stackoverflow.com/a/61575152/7388356UPDATEYou can use Map object introduced in ES6. Basically instead of finding parents by iterating over the array again, you'll...

View Article


Answer by Roko C. Buljan for Build tree array from flat array in javascript

Convert nodes Array to TreeES6 function to convert an Array of nodes (related by parent ID) - to a Tree structure:/** * Convert nodes list related by parent ID - to tree. * @syntax getTree(nodesArray...

View Article


Answer by weiya ou for Build tree array from flat array in javascript

I wrote an ES6 version based on @Halcyon answerconst array = [ { id: '12', parentId: '0', text: 'one-1' }, { id: '6', parentId: '12', text: 'one-1-6' }, { id: '7', parentId: '12', text: 'one-1-7' }, {...

View Article

Answer by zemil for Build tree array from flat array in javascript

My typescript solution, maybe it helps you:type ITreeItem<T> = T & { children: ITreeItem<T>[],};type IItemKey = string | number;function createTree<T>( flatList: T[], idKey:...

View Article

Answer by Oskar Kosowski for Build tree array from flat array in javascript

I had similar issue couple days ago when have to display folder tree from flat array. I didn't see any solution in TypeScript here so I hope it will be helpful.In my cases main parent were only one,...

View Article

Answer by Elias Rabl for Build tree array from flat array in javascript

I've written a test script to evaluate the performance of the two most general solutions (meaning that the input does not have to be sorted beforehand and that the code does not depend on third party...

View Article


Answer by user11415035 for Build tree array from flat array in javascript

You can use this "treeify" package from Github here or NPM.Installation:$ npm install --save-dev treeify-js

View Article

Answer by Cristi Milea for Build tree array from flat array in javascript

this is what i used in a react project// ListToTree.jsimport _filter from 'lodash/filter';import _map from 'lodash/map';export default (arr, parentIdKey) => _map(_filter(arr, ar =>...

View Article


Answer 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

Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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 Article
Browsing all 35 articles
Browse latest View live




Latest Images