列表转换成树形,求助将obj1转换成obj2?

let obj1 = [
  {
    id: null,
    name: "china",
    code: null,
    lnglat: [116.368324, 39.915085],
  },
  {
    id: "01",
    name: "北京",
    code: null,
    lnglat: [116.407526, 39.90403],
  },
  {
    id: "0101",
    name: "北京",
    code: null,
    lnglat: [116.407526, 39.90403],
  },
  {
    id: "010100",
    name: "城区",
    code: "101010100",
    lnglat: [115.365029, 22.778699],
  },
  {
    id: "010200",
    name: "海淀",
    code: "101010200",
    lnglat: [116.307235, 39.892897],
  },
  {
    id: "010300",
    name: "朝阳",
    code: "101010300",
    lnglat: [120.471504, 41.585133],
  },
  {
    id: "010400",
    name: "顺义",
    code: "101010400",
    lnglat: [116.654561, 40.130347],
  },
  {
    id: "010500",
    name: "怀柔",
    code: "101010500",
    lnglat: [116.633717, 40.319141],
  },
  {
    id: "010600",
    name: "通州",
    code: "101010600",
    lnglat: [121.075127, 32.077256],
  },
  {
    id: "010700",
    name: "昌平",
    code: "101010700",
    lnglat: [116.229945, 40.221459],
  },
  {
    id: "010800",
    name: "延庆",
    code: "101010800",
    lnglat: [115.977826, 40.463722],
  },
  {
    id: "010900",
    name: "丰台",
    code: "101010900",
    lnglat: [116.278193, 39.848241],
  },
  {
    id: "011000",
    name: "石景山",
    code: "101011000",
    lnglat: [116.19825, 39.9172],
  },
  {
    id: "011100",
    name: "大兴",
    code: "101011100",
    lnglat: [116.341395, 39.726929],
  },
  {
    id: "011200",
    name: "房山",
    code: "101011200",
    lnglat: [115.981244, 39.705155],
  },
  {
    id: "011300",
    name: "密云",
    code: "101011300",
    lnglat: [116.842353, 40.373251],
  },
  {
    id: "011400",
    name: "门头沟",
    code: "101011400",
    lnglat: [116.037871, 39.932344],
  },
  {
    id: "011500",
    name: "平谷",
    code: "101011500",
    lnglat: [117.119, 40.135987],
  },
  {
    id: "011600",
    name: "东城",
    code: "101011600",
    lnglat: [116.442292, 39.885556],
  },
  {
    id: "011700",
    name: "西城",
    code: "101011700",
    lnglat: [116.368939, 39.889577],
  },
  {
    id: "02",
    name: "上海",
    code: null,
    lnglat: [121.473701, 31.230416],
  },
  {
    id: "0201",
    name: "上海",
    code: null,
    lnglat: [121.473701, 31.230416],
  },
  {
    id: "020100",
    name: "城区",
    code: "101020100",
    lnglat: [115.365029, 22.778699],
  },
  {
    id: "020200",
    name: "闵行",
    code: "101020200",
    lnglat: [121.410704, 31.140333],
  },
  {
    id: "020300",
    name: "宝山",
    code: "101020300",
    lnglat: [121.487468, 31.398589],
  },
  {
    id: "020400",
    name: "黄浦",
    code: "101020400",
    lnglat: [121.499682, 31.215727],
  },
  {
    id: "020500",
    name: "嘉定",
    code: "101020500",
    lnglat: [121.199227, 31.369852],
  },
  {
    id: "020600",
    name: "浦东新区",
    code: "101020600",
    lnglat: [121.544379, 31.221517],
  },
  {
    id: "020700",
    name: "金山",
    code: "101020700",
    lnglat: [121.29995, 30.7321],
  },
  {
    id: "020800",
    name: "青浦",
    code: "101020800",
    lnglat: [121, 31],
  },
  {
    id: "020900",
    name: "松江",
    code: "101020900",
    lnglat: [121.238199, 31.014232],
  },
  {
    id: "021000",
    name: "奉贤",
    code: "101021000",
    lnglat: [121.474042, 30.917795],
  },
  {
    id: "021100",
    name: "崇明",
    code: "101021100",
    lnglat: [121.635058, 31.576855],
  },
  {
    id: "021200",
    name: "徐汇",
    code: "101021200",
    lnglat: [121.419417, 31.166917],
  },
  {
    id: "021300",
    name: "长宁",
    code: "101021300",
    lnglat: [104.91912, 28.577081],
  },
  {
    id: "021400",
    name: "静安",
    code: "101021400",
    lnglat: [121.446911, 31.217425],
  },
  {
    id: "021500",
    name: "普陀",
    code: "101021500",
    lnglat: [122.323297, 29.970571],
  },
  {
    id: "021600",
    name: "虹口",
    code: "101021600",
    lnglat: [121.486502, 31.252971],
  },
  {
    id: "021700",
    name: "杨浦",
    code: "101021700",
    lnglat: [121.544156, 31.263715],
  },
];
let obj2 = [
  {
    name: "china",
    zone: [
      {
        id: "01",
        name: "北京",
        zone: [
          {
            id: "0101",
            name: "北京",
            zone: [
              {
                id: "010100",
                name: "城区",
                code: "101010100",
                lnglat: [115.365029, 22.778699],
              },
              {
                id: "010200",
                name: "海淀",
                code: "101010200",
                lnglat: [116.307235, 39.892897],
              },
              {
                id: "010300",
                name: "朝阳",
                code: "101010300",
                lnglat: [120.471504, 41.585133],
              },
              {
                id: "010400",
                name: "顺义",
                code: "101010400",
                lnglat: [116.654561, 40.130347],
              },
              {
                id: "010500",
                name: "怀柔",
                code: "101010500",
                lnglat: [116.633717, 40.319141],
              },
              {
                id: "010600",
                name: "通州",
                code: "101010600",
                lnglat: [121.075127, 32.077256],
              },
              {
                id: "010700",
                name: "昌平",
                code: "101010700",
                lnglat: [116.229945, 40.221459],
              },
              {
                id: "010800",
                name: "延庆",
                code: "101010800",
                lnglat: [115.977826, 40.463722],
              },
              {
                id: "010900",
                name: "丰台",
                code: "101010900",
                lnglat: [116.278193, 39.848241],
              },
              {
                id: "011000",
                name: "石景山",
                code: "101011000",
                lnglat: [116.19825, 39.9172],
              },
              {
                id: "011100",
                name: "大兴",
                code: "101011100",
                lnglat: [116.341395, 39.726929],
              },
              {
                id: "011200",
                name: "房山",
                code: "101011200",
                lnglat: [115.981244, 39.705155],
              },
              {
                id: "011300",
                name: "密云",
                code: "101011300",
                lnglat: [116.842353, 40.373251],
              },
              {
                id: "011400",
                name: "门头沟",
                code: "101011400",
                lnglat: [116.037871, 39.932344],
              },
              {
                id: "011500",
                name: "平谷",
                code: "101011500",
                lnglat: [117.119, 40.135987],
              },
              {
                id: "011600",
                name: "东城",
                code: "101011600",
                lnglat: [116.442292, 39.885556],
              },
              {
                id: "011700",
                name: "西城",
                code: "101011700",
                lnglat: [116.368939, 39.889577],
              },
            ],
            lnglat: [116.407526, 39.90403],
          },
        ],
        lnglat: [116.407526, 39.90403],
      },
      {
        id: "02",
        name: "上海",
        zone: [
          {
            id: "0201",
            name: "上海",
            zone: [
              {
                id: "020100",
                name: "城区",
                code: "101020100",
                lnglat: [115.365029, 22.778699],
              },
              {
                id: "020200",
                name: "闵行",
                code: "101020200",
                lnglat: [121.410704, 31.140333],
              },
              {
                id: "020300",
                name: "宝山",
                code: "101020300",
                lnglat: [121.487468, 31.398589],
              },
              {
                id: "020400",
                name: "黄浦",
                code: "101020400",
                lnglat: [121.499682, 31.215727],
              },
              {
                id: "020500",
                name: "嘉定",
                code: "101020500",
                lnglat: [121.199227, 31.369852],
              },
              {
                id: "020600",
                name: "浦东新区",
                code: "101020600",
                lnglat: [121.544379, 31.221517],
              },
              {
                id: "020700",
                name: "金山",
                code: "101020700",
                lnglat: [121.29995, 30.7321],
              },
              {
                id: "020800",
                name: "青浦",
                code: "101020800",
                lnglat: [121, 31],
              },
              {
                id: "020900",
                name: "松江",
                code: "101020900",
                lnglat: [121.238199, 31.014232],
              },
              {
                id: "021000",
                name: "奉贤",
                code: "101021000",
                lnglat: [121.474042, 30.917795],
              },
              {
                id: "021100",
                name: "崇明",
                code: "101021100",
                lnglat: [121.635058, 31.576855],
              },
              {
                id: "021200",
                name: "徐汇",
                code: "101021200",
                lnglat: [121.419417, 31.166917],
              },
              {
                id: "021300",
                name: "长宁",
                code: "101021300",
                lnglat: [104.91912, 28.577081],
              },
              {
                id: "021400",
                name: "静安",
                code: "101021400",
                lnglat: [121.446911, 31.217425],
              },
              {
                id: "021500",
                name: "普陀",
                code: "101021500",
                lnglat: [122.323297, 29.970571],
              },
              {
                id: "021600",
                name: "虹口",
                code: "101021600",
                lnglat: [121.486502, 31.252971],
              },
              {
                id: "021700",
                name: "杨浦",
                code: "101021700",
                lnglat: [121.544156, 31.263715],
              },
            ],
            lnglat: [121.473701, 31.230416],
          },
        ],
        lnglat: [121.473701, 31.230416],
      },
    ],
    lnglat: [116.368324, 39.915085],
  },
];
阅读 1.1k
1 个回答
function listToTree(obj1) {
  const result = { name: "china", zone: [] };
  const zoneMap = {};

  obj1.forEach(item => {
    if (item.id === null) {
      return;
    }
    const parentZone = item.id.slice(0, -2);
    if (!zoneMap[parentZone]) {
      zoneMap[parentZone] = { zone: [] };
    }
    zoneMap[item.id] = { ...item, zone: [] };
    zoneMap[parentZone].zone.push(zoneMap[item.id]);
  });

  result.zone = zoneMap[""].zone;
  return [result];
}

const obj2 = listToTree(obj1);
console.log(obj2);
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏