ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
**将一个没有层级的扁平对象,转换为树形结构** * * * * * ~~~ let data = [{ "province": "浙江", "city": "杭州", "name": "西湖" }, { "province": "四川", "city": "成都", "name": "锦里" }, { "province": "四川", "city": "成都", "name": "方所" }, { "province": "四川", "city": "阿坝", "name": "九寨沟" }] ~~~ 转换成如下数据结构: ~~~ let tree = [{ "value": "浙江", "children": [{ "value": "杭州", "children": [{ "value": "西湖" }] }] }, { "value": "四川", "children": [{ "value": "成都", "children": [{ "value": "锦里" }, { "value": "方所" }] }, { "value": "阿坝", "children": [{ "value": "九寨沟" }] }] }] ~~~ 参照`trie`树: ~~~ ~~~