vue 遍歷四維陣列並存儲所需的值
阿新 • • 發佈:2018-11-14
遍歷資料:
[ { "children": [ { "children": [ { "children": [ { "createtime": "2018-10-22 17:01:00", "fatherid": "0A0F402F-670F-4696-9D9C-42F0E0D665A4", "id": "0A0F402F-670F-4696-9D9C-42F0E0D665A5", "level": "4", "name": "Four Program", "type": "prog" } ], "createtime": "2018-10-22 16:36:00", "fatherid": "0A0F402F-670F-4696-9D9C-42F0E0D665A3", "id": "0A0F402F-670F-4696-9D9C-42F0E0D665A4", "level": "3", "name": "Three Program", "type": "prog" } ], "createtime": "2018-10-22 14:21:00", "fatherid": "0A0F402F-670F-4696-9D9C-42F0E0D665A2", "id": "0A0F402F-670F-4696-9D9C-42F0E0D665A3", "level": "2", "name": "Test Program", "type": "prog" } ], "createtime": "2016-06-17 14:32:48", "haschild": 0, "id": "0A0F402F-670F-4696-9D9C-42F0E0D665A2", "level": "1", "name": "My Program", "path": "My Program", "type": "prog" }, { "createtime": "2016-06-17 14:32:48", "haschild": 0, "id": "0A0F402F-670F-4696-9D9C-42F0E0D665A1", "level": "1", "name": "Shared Program", "path": "Shared Program", "type": "prog" }, { "children": [ { "createtime": "2018-10-22 17:15:00", "fatherid": "0A0F402F-670F-4696-9D9C-42F0E0D665A0", "id": "0A0F402F-670F-4696-9D9C-42F0E0D665A6", "level": "2", "name": "Test System program", "type": "prog" } ], "createtime": "2016-06-17 14:32:48", "haschild": 0, "id": "0A0F402F-670F-4696-9D9C-42F0E0D665A0", "level": "1", "name": "System Program", "path": "System Program", "type": "prog" } ]
遍歷程式碼:
//目錄 initCatalog() { let params = { treeType: "prog", } let this_ = this; APITreeList.treeList(params) .then(function (response) { console.log(response) //(3) [{…}, {…}, {…}] /* { label: '一級 1', children: [{ label: '二級 1-1', children: [{ label: '三級 1-1-1' }] }] } * * */ if (response !== null && response.length > 0) { for (let i = 0; i < response.length; i++) { let obj = response[i]; let name = obj.name; let children = obj.children; let id = obj.id; let children2 = []; if (!util.isEmptyObject(children)) { console.log("非空物件-:" + name); children.forEach(function (obj, k) { let id = obj.id; let fatherid = obj.fatherid; let name = obj.name; let children = obj.children; let children3 = []; if (!util.isEmptyObject(children)){ children.forEach(function (obj,k) { let id = obj.id; let fatherid = obj.fatherid; let name = obj.name; let children = obj.children; let children4 = []; if (!util.isEmptyObject(children)){ children.forEach(function (obj,k) { let id = obj.id; let fatherid = obj.fatherid; let name = obj.name; let children = obj.children; children4.push({ label: name, id: id, fatherid:fatherid, children:"", }) }) } children3.push({ label: name, id: id, fatherid:fatherid, children:children4, }) }) } children2.push({ label: name, id: id, fatherid:fatherid, children:children3, }) }); } else { console.log("空物件-:" + name); } this_.treeData.push({ label: name, id: id, children: children2, }) } } }) .catch(function (error) { console.log(error) }) },