From 5cdf6f21a2514eb81b54a9658d80f9d1f860ad42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B2=99=E5=B0=8F=E5=85=89?= <11579539+lizhiqiang_com@user.noreply.gitee.com> Date: Tue, 17 Jun 2025 16:03:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9D=8E=E6=B2=BB=E5=BC=BA=2013296596797?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lizhiqiang.html | 227 ++++++++++++++++++ ...6\250\241\346\235\277.js" => lizhiqiang.js | 44 +++- 2 files changed, 268 insertions(+), 3 deletions(-) create mode 100644 lizhiqiang.html rename "\351\200\222\345\275\222\346\250\241\346\235\277.js" => lizhiqiang.js (74%) diff --git a/lizhiqiang.html b/lizhiqiang.html new file mode 100644 index 0000000..542fa61 --- /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 b54afdc..3c7b584 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 -- Gitee