From a3d47ac8ac463ec5e9373258da371bedcadfe06c Mon Sep 17 00:00:00 2001 From: userName Date: Fri, 30 May 2025 12:17:27 +0800 Subject: [PATCH] =?UTF-8?q?=E7=8E=8B=E8=BD=B2-15826801629?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "\347\216\213\350\275\262.html" | 207 ++++++++++++++++++++++++++++++++ "\347\216\213\350\275\262.js" | 193 +++++++++++++++++++++++++++++ 2 files changed, 400 insertions(+) create mode 100644 "\347\216\213\350\275\262.html" create mode 100644 "\347\216\213\350\275\262.js" diff --git "a/\347\216\213\350\275\262.html" "b/\347\216\213\350\275\262.html" new file mode 100644 index 0000000..a9d0aae --- /dev/null +++ "b/\347\216\213\350\275\262.html" @@ -0,0 +1,207 @@ + + + + + + 轮播图 + + + + + + + + + + \ No newline at end of file diff --git "a/\347\216\213\350\275\262.js" "b/\347\216\213\350\275\262.js" new file mode 100644 index 0000000..e004d21 --- /dev/null +++ "b/\347\216\213\350\275\262.js" @@ -0,0 +1,193 @@ +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) => { + const map = {}; + const result = []; + + // 初始化 map,每个节点结构 + data.forEach(item => { + map[item.value] = { + id: item.value, + title: item.label, + ...(item.parentValue !== undefined && { parentValue: item.parentValue }) + }; + }); + + // 构建树结构 + data.forEach(item => { + const node = map[item.value]; + if (item.parentValue !== undefined) { + const parent = map[item.parentValue]; + if (!parent.child) { + parent.child = []; + } + parent.child.push(node); + } else { + result.push(node); + } + }); + + // 移除 parentValue 字段(递归清理) + const removeParentValue = (nodes) => { + nodes.forEach(node => { + delete node.parentValue; + if (node.child) { + removeParentValue(node.child); + } + }); + }; + + removeParentValue(result); + return result; +}; + +const result = getArea(cityData); +console.log(JSON.stringify(result, null, 4)); -- Gitee