diff --git a/text.html b/text.html new file mode 100644 index 0000000000000000000000000000000000000000..d2a9df1f3c87a98c6b06f6656cf637ff7290cae4 --- /dev/null +++ b/text.html @@ -0,0 +1,123 @@ + + + +
+ + ++ +
+ + + + + + \ No newline at end of file diff --git "a/\351\200\222\345\275\222\346\250\241\346\235\277.js" "b/\351\200\222\345\275\222\346\250\241\346\235\277.js" index b54afdc697f05f876f38581a3e9391f313ccc95d..4b4a39a66bff063d3398b8b6c452df3c7094446c 100644 --- "a/\351\200\222\345\275\222\346\250\241\346\235\277.js" +++ "b/\351\200\222\345\275\222\346\250\241\346\235\277.js" @@ -151,7 +151,34 @@ const cityData = [ // 具体实现 const getArea = (data) => { // 在这里编写代码 -} + // 找到根节点(中国) + const rootNode = data.find(item => item.value === 1); + + // 递归构建树形结构 + const buildTree = (node) => { + // 转换字段:value -> id, label -> title + const treeNode = { + id: node.value, + title: node.label + }; + + // 查找当前节点的所有子节点 + const children = data.filter(item => item.parentValue === node.value); + + // 递归处理子节点 + if (children.length > 0) { + treeNode.child = children.map(child => buildTree(child)); + } + + return treeNode; + }; + + // 清空结果数组并添加根节点 + result.length = 0; + result.push(buildTree(rootNode)); +}; + +// 执行转换 const result = []; getArea(cityData); console.log(result);