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 getting Started Data storage Co...
This article shares with you a uniform motion imp...
Test project: react-demo Clone your react-demo pr...
There are some problems with the compressed versi...
Table of contents Variable type and storage space...
chmod Command Syntax This is the correct syntax w...
During the front-end development process, a situat...
Copy code The code is as follows: <html> &l...
Network security is a very important topic, and t...
When we package the webpackjs file, we introduce ...
Table of contents Problem Analysis Why encapsulat...
Table of contents Preface Ajax serial and paralle...
Change personal account password If ordinary user...
1. Unzip mysql-8.0.21-winx64 2. Configure environ...
Mixins provide a very flexible way to distribute ...