diff --git a/lizhiqiang.html b/lizhiqiang.html new file mode 100644 index 0000000000000000000000000000000000000000..542fa61beb11efa2e178400b8812f222d19c9a67 --- /dev/null +++ b/lizhiqiang.html @@ -0,0 +1,227 @@ + + + + + + + + 完成项目 + + + + +
+
+ +
+
+ + +
+ +
+ + + + + + + \ No newline at end of file diff --git "a/\351\200\222\345\275\222\346\250\241\346\235\277.js" b/lizhiqiang.js similarity index 74% rename from "\351\200\222\345\275\222\346\250\241\346\235\277.js" rename to lizhiqiang.js index b54afdc697f05f876f38581a3e9391f313ccc95d..3c7b584d8b1d58bcd4fbf5a603c8d3000612256f 100644 --- "a/\351\200\222\345\275\222\346\250\241\346\235\277.js" +++ b/lizhiqiang.js @@ -150,8 +150,46 @@ const cityData = [ // 具体实现 const getArea = (data) => { - // 在这里编写代码 -} + // 先找到最顶层的节点(没有parentValue的节点) + const root = data.find(item => !item.parentValue); + + // 递归函数,用于构建树形结构 + const buildTree = (parentId) => { + // 找到所有父节点为当前节点的子节点 + const children = data.filter(item => item.parentValue === parentId); + + // 如果没有子节点,返回null + if (children.length === 0) { + return null; + } + + // 递归处理每个子节点 + return children.map(item => { + // 构建新的节点对象,修改字段名称 + const node = { + id: item.value, + title: item.label + }; + + // 递归查找子节点 + const childNodes = buildTree(item.value); + // 如果有子节点,添加child字段 + if (childNodes) { + node.child = childNodes; + } + + return node; + }); + }; + + // 构建结果数组 + result.push({ + id: root.value, + title: root.label, + child: buildTree(root.value) + }); +}; + const result = []; getArea(cityData); -console.log(result); +console.log(result); \ No newline at end of file