From 2d62bb94f07bd15e3d30057066438ac971ec1171 Mon Sep 17 00:00:00 2001 From: lin yang <763402148@qq.com> Date: Thu, 9 Oct 2025 04:26:34 +0000 Subject: [PATCH] add linyang-13040929373.js. linyang-13040929373 Signed-off-by: lin yang <763402148@qq.com> --- linyang-13040929373.js | 88 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 linyang-13040929373.js diff --git a/linyang-13040929373.js b/linyang-13040929373.js new file mode 100644 index 0000000..285c7cb --- /dev/null +++ b/linyang-13040929373.js @@ -0,0 +1,88 @@ +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, parentId = null) => { + return data.filter(item => item.parentValue === parentId || (parentId === null && !item.parentValue)) .map(item => { + const children = getArea(data, item.value); + const result = { + id: item.value, + title: item.label + }; + if (children.length > 0) { + result.child = children; + } + return result; + }); +}; + +const result = getArea(cityData); +console.log(JSON.stringify(result, null, 2)); -- Gitee