Find parent and child from array javascript
JavaScript
var data = [{ depth: 0, id: "f35vz2f" }, { depth: 0, id: "f359354" }, { depth: 1, id: "f35e0b0", parent_id: "f359354" }, { depth: 2, id: "f35ji24", parent_id: "f35e0b0" }, { depth: 2, id: "f35rnwb", parent_id: "" }, { depth: 2, id: "f35ojh4", parent_id: "f35e0b0" }, { depth: 3, id: "f35lmch", parent_id: "f35ji24" }, { depth: 3, id: "f35kl96", parent_id: "f35ji24" }],
tree = function (data, root) {
var t = {};
data.forEach(o => {
Object.assign(t[o.id] = t[o.id] || {}, o);
t[o.parent_id] = t[o.parent_id] || {};
t[o.parent_id].children = t[o.parent_id].children || [];
t[o.parent_id].children.push(t[o.id]);
});
return t[root].children;
}(data, undefined);
console.log(tree);
Also in JavaScript:
- Title
- crypto.com api
- Category
- JavaScript
- Title
- for in loops javascript
- Category
- JavaScript
- Title
- anagram javascript example
- Category
- JavaScript
- Title
- get the value of a checkbox jquery
- Category
- JavaScript
- Title
- cordova capacitor document viewer fail
- Category
- JavaScript
- Title
- async await javascript
- Category
- JavaScript
- Title
- How to rewrite a function using arrow function syntax
- Category
- JavaScript
- Title
- create a form and submit it dynamically jquery
- Category
- JavaScript
- Title
- angular list contains property
- Category
- JavaScript
- Title
- ISS proxy express
- Category
- JavaScript
- Title
- array vowels js
- Category
- JavaScript
- Title
- add class javascript
- Category
- JavaScript
- Title
- get width of a dom element js
- Category
- JavaScript
- Title
- event.stoppropagation
- Category
- JavaScript
- Title
- how to get a user input in js
- Category
- JavaScript
- Title
- html loop through array
- Category
- JavaScript
- Title
- detect a click outside an element javascript
- Category
- JavaScript
- Title
- iterate object js
- Category
- JavaScript
- Title
- add webpack to react project
- Category
- JavaScript
- Title
- how to add objects in array
- Category
- JavaScript
- Title
- google script get name of google form
- Category
- JavaScript
- Title
- image continuous changing div
- Category
- JavaScript
- Title
- filtering json array in javascript
- Category
- JavaScript
- Title
- change innertext javascript
- Category
- JavaScript
- Title
- angular add debounce time before putting valu in subject next
- Category
- JavaScript
- Title
- daterangepicker set maxdate
- Category
- JavaScript
- Title
- dumpdata json model dajngo
- Category
- JavaScript
- Title
- express server replit
- Category
- JavaScript
- Title
- function js
- Category
- JavaScript
- Title
- average of an array js
- Category
- JavaScript
- Title
- array spread operator in javascript
- Category
- JavaScript
- Title
- error duplicate resources react native
- Category
- JavaScript
- Title
- interaction js drag anddrop
- Category
- JavaScript
- Title
- how to turn decimales into percents with javascript
- Category
- JavaScript
- Title
- angular material toolbar with logo
- Category
- JavaScript
- Title
- how to return the max and min of an array in javascript
- Category
- JavaScript
- Title
- create a react native project
- Category
- JavaScript
- Title
- discord.js tagging the author
- Category
- JavaScript
- Title
- how to sepaarte text in object javascript
- Category
- JavaScript
- Title
- array matching
- Category
- JavaScript
- Title
- event target javascript
- Category
- JavaScript
- Title
- is sublime text a good editor
- Category
- JavaScript
- Title
- dont starve together
- Category
- JavaScript
- Title
- angular pipe first letter uppercase
- Category
- JavaScript
- Title
- how to make a var and sprite in javascript
- Category
- JavaScript
- Title
- date add hours javascript
- Category
- JavaScript
- Title
- import json file javascript
- Category
- JavaScript
- Title
- express and node
- Category
- JavaScript
- Title
- how to update react context inside functional component
- Category
- JavaScript
- Title
- discord.js
- Category
- JavaScript
- Title
- grapejs html editor
- Category
- JavaScript
- Title
- insert into mongodb node js
- Category
- JavaScript
- Title
- global site tag (gtag.js) - google analytics gatsby
- Category
- JavaScript
- Title
- chart js title
- Category
- JavaScript
- Title
- how to loop through array of numbers in javascript
- Category
- JavaScript
- Title
- checked unchecked through js
- Category
- JavaScript
- Title
- express img folder
- Category
- JavaScript
- Title
- get today's date javascript
- Category
- JavaScript
- Title
- defining props in react
- Category
- JavaScript
- Title
- javascript change meta tag
- Category
- JavaScript
- Title
- date to string format javascript
- Category
- JavaScript
- Title
- document ready js
- Category
- JavaScript
- Title
- how to use the foreach method in javascript
- Category
- JavaScript
- Title
- how to get textedit on mac without download
- Category
- JavaScript
- Title
- empty array js
- Category
- JavaScript
- Title
- function increase a number by click javascript
- Category
- JavaScript
- Title
- docker node label add
- Category
- JavaScript
- Title
- angularjs make post request
- Category
- JavaScript
- Title
- discord js get badge user
- Category
- JavaScript
- Title
- add to json object javascript
- Category
- JavaScript
- Title
- How to insert an element after another element in JavaScript without using a library
- Category
- JavaScript
- Title
- get all entries in object as array hjs
- Category
- JavaScript
- Title
- jacvascript parse float
- Category
- JavaScript
- Title
- create array javascript
- Category
- JavaScript
- Title
- create element javascript with id
- Category
- JavaScript
- Title
- how to get last item in array javascript
- Category
- JavaScript
- Title
- check if localstorage key exists js
- Category
- JavaScript
- Title
- create element javascript with class
- Category
- JavaScript
- Title
- check items in array javascript
- Category
- JavaScript
- Title
- data-widget="pushmenu" collable automatically
- Category
- JavaScript
- Title
- how to add object to array javascript
- Category
- JavaScript
- Title
- $pull mongoose
- Category
- JavaScript
- Title
- datatables server side
- Category
- JavaScript
- Title
- cascading dropdown for forms react
- Category
- JavaScript
- Title
- javascript change color
- Category
- JavaScript
- Title
- javascript check if date is today
- Category
- JavaScript
- Title
- boucle for in js
- Category
- JavaScript
- Title
- discord.js reply to message
- Category
- JavaScript
- Title
- axios delete request payload
- Category
- JavaScript
- Title
- * http://sizzlejs.com/
- Category
- JavaScript
- Title
- create object javascript
- Category
- JavaScript
- Title
- insertion sort js
- Category
- JavaScript
- Title
- cache buster in angular application
- Category
- JavaScript
- Title
- javascript check for undefined
- Category
- JavaScript
- Title
- in javascript how to split string
- Category
- JavaScript
- Title
- how to use function in javascript
- Category
- JavaScript
- Title
- crear componente con angular cli
- Category
- JavaScript
- Title
- fullcalendar reload events ajax
- Category
- JavaScript
- Title
- combine values of address line 1 and address line 2 javascript
- Category
- JavaScript
- Title
- change text of element js
- Category
- JavaScript