1. DemandThe backend provides such data for the front end to convert into a tree structure (without duplicate data). Without further ado, let's first take a look at what kind of array data is given and what kind of tree structure it is converted into. The array sent by the server const arr = [ [ {"deptId":"D019", "deptName":"Sales Department"}, {"deptId":"D019101", "deptName":"North China Sales Center"} ],[ {"deptId":"D083", "deptName":"Music Department"} ],[ {"deptId":"D027", "deptName":"Hangzhou Research Institute"}, {"deptId":"D027048", "deptName":"Technical Engineering Division"}, {"deptId":"D027048002", "deptName":"Project Management Center"} ],[ {"deptId":"D027", "deptName":"Hangzhou Research Institute"}, {"deptId":"D027048", "deptName":"Technical Engineering Division"} ],[ {"deptId":"D027", "deptName":"Hangzhou Research Institute"}, {"deptId":"D027048", "deptName":"Technical Engineering Division"} ] ] Finally converted to const arr = [ { deptId: 'D019', deptName: 'Sales Department', children: [{ deptId: 'D019101', deptName: 'North China Sales Center', children: [], }] }, { deptId: 'D083', deptName: 'Music Department', children: [] }, { deptId: 'D027', deptName: 'Hangzhou Research Institute', children: [{ deptId: 'D027048', deptName: 'Technical Engineering Division', children: [{ deptId: 'D027048002', deptName: 'Project Management Center', children: [] }] }] }, ] 2. Code (developed in reactHooks)const [treeData, setTreeData] = useState([]); console.log(treeData); //treeData is the final tree structure required (printed in my local browser is correct) useEffect(() => { : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : const arr = JSON.parse(str).flat(); //Flattening let newArr = []; noRepeat(arr).length && noRepeat(arr).forEach(it => { appendChild(it, newArr); }); }, []) const noRepeat = (arr) => { //Remove duplicates let newobj = {}; return arr.reduce((preVal, curVal) => { newobj[curVal.deptId] ? '' : newobj[curVal.deptId] = preVal.push(curVal); return preVal }, []); } const appendChild = (item, newArr) => { if(!newArr.find(it => item.deptId.indexOf(it.deptId) > -1)) { //All first-level departments newArr.push({ deptId: item.deptId, deptName: item.deptName, children: [], }); setTreeData(newArr); }else { appendOtherChild(item, newArr); } } const appendOtherChild = (item, newArr) => { newArr.map(it => { if(item.deptId.indexOf(it.deptId) > -1 && item.deptId.length === it.deptId.length+3) { it.children.push({ deptId: item.deptId, deptName: item.deptName, children: [], }) }else { appendOtherChild(item, it.children); } }); setTreeData(newArr); } SummarizeMaybe these data are different from yours, but the logic is probably pretty close. You can take a good look at these dozens of lines of code. This is the end of this article on how to convert JavaScript arrays into tree structures. For more information on converting JavaScript arrays into tree structures, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Detailed installation and uninstallation tutorial for MySQL 8.0.12
Table of contents About Kubernetes Basic environm...
6 solutions for network failure in Docker contain...
Test: Chrome v80.0.3987.122 is normal There are t...
1. The startup menu is to move the cursor to the ...
0. Why do we need remote development? When develo...
Problem background: There is a requirement to tak...
Preface During the development process, we someti...
1. Introduction to mysqldump mysqldump is a logic...
Treemaps are mainly used to visualize tree-like d...
body{font-size:12px; font-family:"宋体";}...
MySQL implements Oracle-like sequences Oracle gen...
The detailed steps for installing mysql5.7.19 on ...
Table of contents 1. JavaScript is single-threade...
Table of contents 1. Self-enumerable properties 2...
Preface: I believe that those who need to underst...