var str = `北京 2
河北 3
河北-邯郸 301
河北-保定 302
河北-邢台 303
山东 4
山东-青岛 401
山东-烟台 402`;
var arrStr = str.split("\n");
var result = [];
arrStr.map(i => {
  let = item = i.split(" ");
  if (i.indexOf("-") == -1) {
    result.push({
      label: item[0],
      value: item[1],
      key: item[1],
      children: []
    })
  } else {
    let city = item[0].split("-");
    let cur = result.find(j => j.label == city[0])
    cur.children.push({
      label: city[1],
      value: item[1],
      key: item[1],
    })
  }
})
console.log(result)
console.log(JSON.stringify(result))

  

相关文章:

  • 2022-12-23
  • 2021-03-26
  • 2021-10-26
  • 2021-11-20
  • 2021-12-16
  • 2021-09-22
  • 2021-05-28
  • 2022-12-23
猜你喜欢
  • 2021-07-23
  • 2021-12-04
  • 2022-12-23
  • 2021-10-15
  • 2021-11-08
  • 2022-01-17
  • 2022-12-23
相关资源
相似解决方案