1.json樹數據查找所有父級--完成
json:樹結構數據
var arrData = [{ "label": "中國", "City": null, "value": "0", "children": [{ "label": "河北", "City": "0", "value": "1", "children": [{ "label": "石家庄", "City": "1", "value": "1.1", "children": null }, { "label": "保定", "City": "1", "value": "1.2", "children": null }, { "label": "邯鄲", "City": "1", "value": "1.3", "children": [{ "label": "邯山區", "City": "1.3", "value": "1.3.1", "children": [{ "label": "叢西街道", "City": "1.3.1", "value": "1.3.1.1", "children": null }] }, { "label": "涉縣", "City": "1.3", "value": "1.3.2", "children": null }, { "label": "叢台區", "City": "1.3", "value": "1.3.3", "children": null } ] } ] }, { "label": "山東", "City": "0", "value": "2", "children": [{ "label": "濟南", "City": "2", "value": "2.1", "children": null }] }, { "label": "北京", "City": "0", "value": "3", "children": null } ] }];
遞歸查找父級數據
// data:json nodeId:節點,arrRes:[] 返回的數據 function getParentNode(data, nodeId, arrRes) { if (!data ) { if (!nodeId) { arrRes.unshift(nodeId); } return arrRes; } for (var i = 0, length = data.length; i < length; i++) { let node = data[i]; if (node.value == nodeId) { arrRes.unshift(nodeId); console.log(arrData); getParentNode(arrData, node.City, arrRes); break; } else { if (!!node.children) { getParentNode(node.children, nodeId, arrRes); } } } return arrRes; };
調用: var res = getParentNode(arrData, '1.3.1', []);
2.再次修改后:
// data:json, nodeId:節點 function getParent(data2, nodeId2) { var arrRes = []; if (data2.length == 0) { if (!!nodeId2) { arrRes.unshift(nodeId2); } return arrRes; } let rev = (data, nodeId) => { for (var i = 0, length = data.length; i < length; i++) { let node = data[i]; if (node.value == nodeId) { arrRes.unshift(nodeId); rev(data2, node.City); break; } else { if (!!node.children) { rev(node.children, nodeId); } } } return arrRes; }; arrRes = rev(data2, nodeId2); return arrRes; }
調用:var res = getParent(arrData, '1.3.1');
3. 正則表達式 查找的,遞歸20級深度的;
var getValue=(function () { var arrData = [{ "label": "中國", "City": null, "value": "0", "children": [{ "label": "河北", "City": "0", "value": "hb", "children": [{ "label": "石家庄", "City": "1", "value": "1.1", "children": null }, { "label": "保定", "City": "1", "value": "1.2", "children": null }, { "label": "邯鄲", "City": "n", "value": "bk", "children": [{ "label": "邯山區", "City": "bk", "value": "1.3.1", "children": [{ "label": "叢西街道", "City": "1.3.1", "value": "1.3.1.1", "children": null }] }, { "label": "涉縣", "City": "1.3", "value": "1.3.2", "children": null }, { "label": "叢台區", "City": "1.3", "value": "1.3.3", "children": null } ] } ] }, { "label": "山東", "City": "0", "value": "2", "children": [{ "label": "濟南", "City": "2", "value": "2.1", "children": null }] }, { "label": "北京", "City": "0", "value": "3", "children": null } ] }]; console.log(JSON.stringify(arrData[0])) return function getValue(values){ var arrs=[]; var values=values; var c=JSON.stringify(arrData[0]); var keys=''; if(arguments[0]&&c.search(values)!=-1){ for(var i=0;i<20;i++){ var newReg=new RegExp('\"City\"\:\"([^\"]+)\"\,\"value\"\:\"'+values); if(values!=keys){ arrs.unshift(values) } if(c.search(newReg)!=-1){ keys=c.match(newReg)[0].replace(newReg,'$1') arrs.unshift(keys); values=keys; }else{ arrs.unshift(null) return arrs; } }}else{ return values } } })(); console.log(getValue('1.3.1'))
4.Cascader 級聯選擇 組件 v-model的數據是一個數組類型,工作中如果需要回顯的話,就需要傳遞所有父級元素的ID所組成的數組,但是后台只存放了目標元素的ID,所以只能自己去遍歷數據獲取了
用遞歸查找到ID的所屬元素,然后把每一級的parentId一起返回。
轉:https://blog.csdn.net/weixin_42869548/article/details/82012316
[{ "orgId": 1, "orgName": "校長辦公室1", "parentId": 0, "children": [{ "orgId": 2, "orgName": "校長辦公室2", "parentId": 1, "children": [{ "orgId": 3, "orgName": "校長辦公室3", "parentId": 2, }] }] }]
function buildParentList(arr){ arr.forEach(g => { if(g.parentId != undefined) { let pid = g['parentId'] let oid = g['orgId'] parentList[oid] = pid } if (g.children != undefined) buildParentList(g['children']) }) } function findParent(idx){ if (parentList[idx] != undefined){ let pid = parentList[idx] console.log(pid) findParent(pid) } } buildParentList(list) findParent(3); // 0 1 2 findParent(2); // 0 1 findParent(4); // undefined
5.從上往下找下的
function getParents(data, id) { for (var i in data) { if (data[i].value == id) { return []; } if (data[i].children) { var ro = getParents(data[i].children, value); if (ro !== undefined) return ro.concat(data[i].id); } } console.log(ro); }
