关于数据操作的代码优化

城市下面有很多服务站需要联动效果的数据
服务端全部返成以为给我,我的代码如下,怎么优化

export const dealDistribution = (data) => {
  const treeData = []
  data.forEach(item => {
    const isExit = treeData.find(treeItem => treeItem.cityCode === item.cityCode)
    if (!isExit) {
      treeData.push({
        cityCode: item.cityCode,
        cityName: item.cityName,
        pickhouseList: []
      })
    }
  })
  treeData.map(treeItem => {
    data.map(dataItem => {
      if (treeItem.cityCode === dataItem.cityCode) {
        treeItem.pickhouseList.push({
          pickhouseId: dataItem.id,
          pickhouseName: dataItem.name
        })
      }
    })
  })
  return treeData
}
阅读 940
1 个回答

我觉得是数据库设计的问题

1、城市数据

[
    {
        "cityName": xxx,
        "cityCode": xxx,
    },
    {
        "cityName": xxx,
        "cityCode": xxx,
    },
]

选择城市,然后在向服务端请求下属的服务站。

2、城市下的服务站的数据

服务端根据 cityCode 去查找下属的服务站。

{
     "cityName": xxx,
     "cityCode": xxx,
     "sub": [
            {
                "name": xxx,
                "code": xxx
            }
     ]
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题