From 174b4fa3dc60a5a978947119d8d0c1fd4c4bc9e3 Mon Sep 17 00:00:00 2001 From: cui <2306503360@qq.com> Date: Thu, 5 Jun 2025 12:42:05 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BD=AE=E6=92=AD=E5=9B=BE=EF=BC=9A=E5=B4=94?= =?UTF-8?q?=E4=BD=B3=E6=85=A7+18712872980?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cuijiahui.html | 257 +++++++++++++++++++++++++++++++++++++++++++++++++ cuijiahui.js | 203 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 460 insertions(+) create mode 100644 cuijiahui.html create mode 100644 cuijiahui.js diff --git a/cuijiahui.html b/cuijiahui.html new file mode 100644 index 0000000..f639dd1 --- /dev/null +++ b/cuijiahui.html @@ -0,0 +1,257 @@ + + + + + + 轮播图 - Claude + + + + + + + + \ No newline at end of file diff --git a/cuijiahui.js b/cuijiahui.js new file mode 100644 index 0000000..0659222 --- /dev/null +++ b/cuijiahui.js @@ -0,0 +1,203 @@ +const cityData = [ + { + parentValue: 1, + value: 11, + label: '北京市', + }, + { + parentValue: 11, + value: 111, + label: '东城区' + }, + { + parentValue: 11, + value: 112, + label: '西城区' + }, + { + parentValue: 1, + value: 12, + label: '上海市', + }, + { + parentValue: 12, + value: 121, + label: '黄浦区' + }, + { + parentValue: 12, + value: 122, + label: '徐汇区' + }, + { + parentValue: 1, + value: 13, + label: '广东省', + }, + { + parentValue: 13, + value: 131, + label: '广州市', + }, + { + parentValue: 131, + value: 1311, + label: '天河区' + }, + { + parentValue: 131, + value: 1312, + label: '越秀区' + }, + { + parentValue: 132, + value: 1321, + label: '南山区' + }, + { + parentValue: 132, + value: 1322, + label: '罗湖区' + }, + { + value: 1, + label: '中国', + }, + { + parentValue: 13, + value: 132, + label: '深圳市', + } +] + +/* * + 1.请利用递归方法实现一个函数,把上方数据递归成有层级的数据, + 2.把value字段变成id字段,label字段变成title字段,children字段变成child字段, + 3.去掉parentValue字段,最终得到以下数据。需要一模一样的数据 + ⚠️注意:不允许使用三层循环,请使用递归方法 + [ + { + id: 1, + title: '中国', + child: [ + { + id: 11, + title: '北京市', + child: [ + { + id: 111, + title: '东城区' + }, + { + id: 112, + title: '西城区' + } + ] + }, + { + id: 12, + title: '上海市', + child: [ + { + id: 121, + title: '黄浦区' + }, + { + id: 122, + title: '徐汇区' + } + ] + }, + { + id: 13, + title: '广东省', + child: [ + { + id: 131, + title: '广州市', + child: [ + { + id: 1311, + title: '天河区' + }, + { + id: 1312, + title: '越秀区' + } + ] + }, + { + id: 132, + title: '深圳市', + child: [ + { + id: 1321, + title: '南山区' + }, + { + id: 1322, + title: '罗湖区' + } + ] + } + ] + } + ] + } +] + * */ + +// 具体实现 +const getArea = (data) => { + // 递归函数:根据父级ID查找并构建子级数据 + const buildChildren = (parentId) => { + const children = []; + + // 遍历原始数据,找到所有父级为parentId的项目 + for (let item of data) { + if (item.parentValue === parentId) { + // 构建新的节点对象 + const node = { + id: item.value, + title: item.label + }; + + // 递归查找当前节点的子级 + const childNodes = buildChildren(item.value); + + // 如果有子级,添加child字段 + if (childNodes.length > 0) { + node.child = childNodes; + } + + children.push(node); + } + } + + return children; + }; + + // 找到根节点(没有parentValue的节点) + const rootNodes = []; + for (let item of data) { + if (!item.parentValue) { + const rootNode = { + id: item.value, + title: item.label + }; + + // 递归构建根节点的子级 + const children = buildChildren(item.value); + if (children.length > 0) { + rootNode.child = children; + } + + rootNodes.push(rootNode); + } + } + + return rootNodes; +} + +const result = getArea(cityData); +console.log(JSON.stringify(result, null, 2)); \ No newline at end of file -- Gitee