JavaScript根据城市编号递归查询省市区汉字

现有字符串"120000,120100,120104",我们需要的到"天津市天津市南开区"的字符串。

原始数据结构,常用于地区下拉组件,三级联选,如下

export const addressOptions = [
  {
  "value": "110000",
  "label": "北京市",
  "children": [{
    "value": "110100",
    "label": "北京市",
    "children": [{
      "value": "110101",
      "label": "东城区"
    }, {
      "value": "110102",
      "label": "西城区"
    }, {
      "value": "110105",
      "label": "朝阳区"
    }, {
      "value": "110106",
      "label": "丰台区"
    }, {
      "value": "110107",
      "label": "石景山区"
    }, {
      "value": "110108",
      "label": "海淀区"
    }, {
      "value": "110109",
      "label": "门头沟区"
    }, {
      "value": "110111",
      "label": "房山区"
    }, {
      "value": "110112",
      "label": "通州区"
    }, {
      "value": "110113",
      "label": "顺义区"
    }, {
      "value": "110114",
      "label": "昌平区"
    }, {
      "value": "110115",
      "label": "大兴区"
    }, {
      "value": "110116",
      "label": "怀柔区"
    }, {
      "value": "110117",
      "label": "平谷区"
    }, {
      "value": "110118",
      "label": "密云区"
    }, {
      "value": "110119",
      "label": "延庆区"
    }]
  }]
}, {
  "value": "120000",
  "label": "天津市",
  "children": [{
    "value": "120100",
    "label": "天津市",
    "children": [{
      "value": "120101",
      "label": "和平区"
    }, {
      "value": "120102",
      "label": "河东区"
    }, {
      "value": "120103",
      "label": "河西区"
    }, {
      "value": "120104",
      "label": "南开区"
    }, {
      "value": "120105",
      "label": "河北区"
    }, {
      "value": "120106",
      "label": "红桥区"
    }, {
      "value": "120110",
      "label": "东丽区"
    }, {
      "value": "120111",
      "label": "西青区"
    }, {
      "value": "120112",
      "label": "津南区"
    }, {
      "value": "120113",
      "label": "北辰区"
    }, {
      "value": "120114",
      "label": "武清区"
    }, {
      "value": "120115",
      "label": "宝坻区"
    }, {
      "value": "120116",
      "label": "滨海新区"
    }, {
      "value": "120117",
      "label": "宁河区"
    }, {
      "value": "120118",
      "label": "静海区"
    }, {
      "value": "120119",
      "label": "蓟州区"
    }]
  }]
},]

根据给定的行政区划代码字符串"120000,120100,120104"从这个结构中获取完整的地址名称,可以编写一个递归函数。


/**
 * 根据3串6位数字获取地址
 * @param codeString
 * @param options
 * @returns {string}
 */
function getAddressNamesByCode(codeString, options = addressOptions) {
  let names = [];
  // 从字符串中提取行政区划代码序列
  const codeArray = codeString.split(',');
  // 递归查找函数
  function findAddress(name, option, codes) {
    if (codes.length === 0 || !option.children) {
      return;
    }
    const targetCode = codes[0];
    const childOption = option.children.find(child => child.value === targetCode);
    if (childOption) {
      names.push(childOption.label);
      findAddress(childOption.label, childOption, codes.slice(1));
    }
  }
  // 开始查找
  findAddress('', { children: options }, codeArray);
  // 返回连接后的地址名称
  return names.join(' ');
}

// 使用函数
console.log(getAddressNamesByCode("120000,120100,120104")); 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值