From 9b26346155ab4ce0bafca7c2708b7e02e830314c Mon Sep 17 00:00:00 2001 From: your_username Date: Wed, 4 Jun 2025 15:42:26 +0800 Subject: [PATCH] =?UTF-8?q?'=E6=9D=8E=E4=BF=8A=E6=9D=B013297597292'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lijunjie.html | 219 ++++++++++++++++++++++++++++++++++++++++++++++++++ lijunjie.js | 108 +++++++++++++++++++++++++ 2 files changed, 327 insertions(+) create mode 100644 lijunjie.html create mode 100644 lijunjie.js diff --git a/lijunjie.html b/lijunjie.html new file mode 100644 index 0000000..4568072 --- /dev/null +++ b/lijunjie.html @@ -0,0 +1,219 @@ + + + + + 轮播图 + + + +
+
+
+ 图片1 +
+
+ 图片2 +
+
+ 图片3 +
+
+ 图片4 +
+
+ 图片5 +
+
+ + +
+
+ + + + + diff --git a/lijunjie.js b/lijunjie.js new file mode 100644 index 0000000..89c30da --- /dev/null +++ b/lijunjie.js @@ -0,0 +1,108 @@ +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: '深圳市', + } +] + +// 具体实现 +const getArea = (data) => { + const result = []; + + // 找到根节点并添加到结果中 + const root = data.find(item => !item.parentValue); + if (root) { + const rootNode = { + id: root.value, + title: root.label, + child: [] + }; + result.push(rootNode); + + // 递归查找子节点 + const findChildren = (parent) => { + const children = data.filter(item => item.parentValue === parent.id); + children.forEach(item => { + const node = { + id: item.value, + title: item.label, + child: [] + }; + parent.child.push(node); + findChildren(node); + }); + }; + + findChildren(rootNode); + } + + return result; +} + +const result = getArea(cityData); +console.log(JSON.stringify(result, null, 2)); \ No newline at end of file -- Gitee