Find parent and child from array 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:
- Errors messages produced from JavaScript data validation.\
- gulp serve Cannot find module 'gulp-sass'
- create element javascript with class
- js before unload
- fetch api in js
- check data type in javascript
- javascript remove last character from a string
- javascript loop through array backwords
- switch case mdn
- random array javascript
- document ready js
- : Cannot set the body fields of a Request with content-type "application/json".
- set node process production
- clone javascript object
- npm -g
- app.js:38650 [Vue warn]: Failed to mount component: template or render function not defined
- javascript change select options dynamically
- react route props
- pd.read_json('data.json') args
- count characters in typescript
- use strict javascript
- send data in datatable ajax
- console log javascript
- javascript select element with two classes