Home  >  Q&A  >  body text

javascript - Data format conversion, please help

    110000:['北京市', 1],
    110100:['北京市', 110000],
    110101:['东城区', 110100],
    110102:['西城区', 110100],
    110105:['朝阳区', 110100],
    110106:['丰台区', 110100],
    110107:['石景山区', 110100],
    110108:['海淀区', 110100],
    110109:['门头沟区', 110100],
    110111:['房山区', 110100],
    110112:['通州区', 110100],
    110113:['顺义区', 110100],
    110114:['昌平区', 110100],
    110115:['大兴区', 110100],
    110116:['怀柔区', 110100],
    110117:['平谷区', 110100],
    110199:['其它区', 110100],
    110200:['县', 110000],
    110228:['密云县', 110200],
    110229:['延庆县', 110200],

How to convert to:

"86": {
    "110000": "北京市",
    "120000": "天津市",
    "130000": "河北省",
    "140000": "山西省",
    "150000": "内蒙古自治区",
    "210000": "辽宁省",
    "220000": "吉林省",
    "230000": "黑龙江省",
    "310000": "上海市",
    "320000": "江苏省",
    "330000": "浙江省",
    "340000": "安徽省",
    "350000": "福建省",
    "360000": "江西省",
    "370000": "山东省",
    "410000": "河南省",
    "420000": "湖北省",
    "430000": "湖南省",
    "440000": "广东省",
    "450000": "广西壮族自治区",
    "460000": "海南省",
    "500000": "重庆市",
    "510000": "四川省",
    "520000": "贵州省",
    "530000": "云南省",
    "540000": "西藏自治区",
    "610000": "陕西省",
    "620000": "甘肃省",
    "630000": "青海省",
    "640000": "宁夏回族自治区",
    "650000": "新疆维吾尔自治区",
    "710000": "台湾省",
    "810000": "香港特别行政区",
    "820000": "澳门特别行政区"
  },
  "110000": {
    "110100": "市辖区"
  },
  "110100": {
    "110101": "东城区",
    "110102": "西城区",
    "110105": "朝阳区",
    "110106": "丰台区",
    "110107": "石景山区",
    "110108": "海淀区",
    "110109": "门头沟区",
    "110111": "房山区",
    "110112": "通州区",
    "110113": "顺义区",
    "110114": "昌平区",
    "110115": "大兴区",
    "110116": "怀柔区",
    "110117": "平谷区",
    "110228": "密云县",
    "110229": "延庆县"
  }

What about this format? My ability is limited and I really can’t figure it out. Can this kind of transformation be achieved?

伊谢尔伦伊谢尔伦2656 days ago834

reply all(3)I'll reply

  • 扔个三星炸死你

    扔个三星炸死你2017-06-12 09:31:38

    I can’t give you the code for the mobile phone answer, but the general idea should be:
    1. Let’s sort out your needs first. If I understand it wrong, you don’t need to read the following:
    You want to make the original one-dimensional , data without hierarchical structure is separated according to country code, province and city code, and region code. The key of the lower-level region is the key of the upper-level province and city. Through the code, you can directly go from country to province to city, level by level. Find a region.
    2. First of all, your data is an object. Each attribute is the region code of the corresponding value, and the value is an array. The second element is the code of its parent node. This is easy to handle.
    3. First create an empty object.
    4. Loop through the original object.
    5. Every time you get an attribute of the original object, first judge the second value of its value (that is, the binary array). If it is 1, it means it is a top-level region, that is, a province or a municipality. Write these to the new When creating the object, write it under the attribute 86 country code,
    6. If the second value you get is not 1, it means that it is not a top-level province or city, but a certain region. Go to the new object to find out if there is such a If the province or region corresponding to the code does not exist, create it in the new object. If it exists, write it directly to the corresponding code.
    7. In this way, it can be divided according to the country~province/municipality~region.
    8. I see that you also specifically marked the municipality. According to the data you gave, if the name of the province and the name of the municipality are the same, it means it is Just the municipality is fine

    9. When calling on a mobile phone, there will be many things that are difficult to understand. If the method is unclear or wrong, please ignore it.

    reply
    0
  • ringa_lee

    ringa_lee2017-06-12 09:31:38

    A simple example is as follows (the source data format is not JSON, so eval is forced in the simple implementation):

    const src = `
      110000: ['北京市', 1],
      110100: ['北京市', 110000],
      110101: ['东城区', 110100],
      110102: ['西城区', 110100],
      110105: ['朝阳区', 110100],
      110106: ['丰台区', 110100],
      110107: ['石景山区', 110100],
      110108: ['海淀区', 110100],
      110109: ['门头沟区', 110100],
      110111: ['房山区', 110100],
      110112: ['通州区', 110100],
      110113: ['顺义区', 110100],
      110114: ['昌平区', 110100],
      110115: ['大兴区', 110100],
      110116: ['怀柔区', 110100],
      110117: ['平谷区', 110100],
      110199: ['其它区', 110100],
      110200: ['县', 110000],
      110228: ['密云县', 110200],
      110229: ['延庆县', 110200]
    `
    
    function convert (src) {
      // hack 将结构字符串 eval 转为对象
      const tmpObj = eval('(function () { return {' + src + '} })()')
    
      // 输出目标格式,硬编码一条简单数据
      const result = { 86: {}, 110000: { 110100: '市辖区' }, 110100: {} }
      // [110000, 110100...]
      const tmpArr = Object.keys(tmpObj)
      tmpArr.forEach(key => {
        result[86][key] = tmpObj[key][0]
      })
      tmpArr.forEach(key => {
        if (tmpObj[key][1] === 110100) {
          result[110100][key] = tmpObj[key][0]
        }
      })
      return result
    }
    
    console.log(convert(src))

    Execution result:

    ➜  Desktop node demo
    { '86': 
       { '110000': '北京市',
         '110100': '北京市',
         '110101': '东城区',
         '110102': '西城区',
         '110105': '朝阳区',
         '110106': '丰台区',
         '110107': '石景山区',
         '110108': '海淀区',
         '110109': '门头沟区',
         '110111': '房山区',
         '110112': '通州区',
         '110113': '顺义区',
         '110114': '昌平区',
         '110115': '大兴区',
         '110116': '怀柔区',
         '110117': '平谷区',
         '110199': '其它区',
         '110200': '县',
         '110228': '密云县',
         '110229': '延庆县' },
      '110000': { '110100': '市辖区' },
      '110100': 
       { '110101': '东城区',
         '110102': '西城区',
         '110105': '朝阳区',
         '110106': '丰台区',
         '110107': '石景山区',
         '110108': '海淀区',
         '110109': '门头沟区',
         '110111': '房山区',
         '110112': '通州区',
         '110113': '顺义区',
         '110114': '昌平区',
         '110115': '大兴区',
         '110116': '怀柔区',
         '110117': '平谷区',
         '110199': '其它区' } }

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-06-12 09:31:38

    https://windqyoung.github.io/...

    Convert at will.

    reply
    0
  • Cancelreply