浅谈对一类接口数据的处理

2019-05-192781

浅谈对一类接口数据的处理

最近在进行微鱼项目接口的对接时,拿到过这样一类接口的数据,感觉处理起来比较麻烦。特地在此拿出来说一下,并分享一下自己的处理方法。若大家有更好的想法,欢迎讨论~~

接口数据

以下是从接口处拿到的数据,一共有两个列表:

  • area_list

    let area_list = [
      {area_id: 1, name: "测试地区", parent_id: 0, ancestor: "[]"},
      {area_id: 23, name: "测试省", parent_id: 0, ancestor: "[]"},
      {area_id: 24, name: "测试虚拟市A", parent_id: 23, ancestor: "[23]"},
      {area_id: 25, name: "测试虚拟市B", parent_id: 23, ancestor: "[23]"},
      {area_id: 26, name: "测试虚拟县A", parent_id: 25, ancestor: "[23, 25]"},
      {area_id: 27, name: "测试虚拟县b", parent_id: 25, ancestor: "[23, 25]"},
      {area_id: 28, name: "广东省", parent_id: 0, ancestor: "[]"},
      {area_id: 29, name: "深圳市", parent_id: 28, ancestor: "[28]"},
      {area_id: 30, name: "佛山市", parent_id: 28, ancestor: "[28]"},
      {area_id: 31, name: "广州市", parent_id: 28, ancestor: "[28]"},
      {area_id: 32, name: "广百新一城", parent_id: 31, ancestor: "[28, 31]"},
      {area_id: 33, name: "天河城百货", parent_id: 31, ancestor: "[28, 31]"},
      {area_id: 34, name: "布吉万象汇", parent_id: 29, ancestor: "[28, 29]"},
      {area_id: 35, name: "喜荟城", parent_id: 31, ancestor: "[28, 31]"},
      {area_id: 36, name: "喜荟城", parent_id: 31, ancestor: "[28, 31]"},
      {area_id: 37, name: "万达广场", parent_id: 31, ancestor: "[28, 31]"},
      {area_id: 38, name: "虚拟商城", parent_id: 25, ancestor: "[23, 25]"},
      {area_id: 39, name: "虚拟商场2", parent_id: 24, ancestor: "[23, 24]"},
      {area_id: 40, name: "海岸城", parent_id: 29, ancestor: "[28, 29]"},
      {area_id: 41, name: "万象汇", parent_id: 29, ancestor: "[28, 29]"},
      {area_id: 42, name: "钻汇广场", parent_id: 31, ancestor: "[28, 31]"},
      {area_id: 44, name: "1", parent_id: 31, ancestor: "[28, 31]"},
      {area_id: 45, name: "1", parent_id: 31, ancestor: "[28, 31]"},
      {area_id: 46, name: "2", parent_id: 29, ancestor: "[28, 29]"},
      {area_id: 47, name: "测试商场", parent_id: 29, ancestor: "[28, 29]"},
      {area_id: 49, name: "万达广场", parent_id: 30, ancestor: "[28, 30]"},
      {area_id: 50, name: "万达广场", parent_id: 30, ancestor: "[28, 30]"}
    ]
  • location_list

    let location_list = [
      {location_id: 3, area_id: 1, position: "前厅点位c"},
      {location_id: 10, area_id: 33, position: "4楼游戏厅B2"},
      {location_id: 9, area_id: 34, position: "负一楼B2"},
      {location_id: 11, area_id: 40, position: "二楼"},
      {location_id: 12, area_id: 41, position: "A1"},
      {location_id: 13, area_id: 42, position: "1楼A区"},
      {location_id: 15, area_id: 47, position: "1"},
      {location_id: 18, area_id: 50, position: "4楼厕所旁"}
    ]

    要求是酱紫的:

举个栗子:

location_list 里的 {location_id: 13, area_id: 42, position: "1楼A区"},

这个area_id: 42 就是匹配上面area_list列表中的area_id为42的数据,即 钻汇广场

但是area_id: 42 也有 parent_id: 31 的数据,所以要拿到 area_id: 31 的数据,即 广州市

同理,area_id: 31 也有 parent_id: 28 的数据,即 广东省

一直到 parent_id: 0 为止。

最终要拼接为:广东省广州市钻汇广场1楼A区

对于这样的两个列表,难免让头一次接触的我感到木知所措。苦思良酒,才给了相关的处理方法。

处理方法

起初,我是打算先构造一个函数来直接通过area_id获取列表中对应的对象的,就像这样:

let objFunc = (area_id) => {
    for (let i = 0; i < area_list.length; i++) {
        if (area_list[i].area_id == area_id)
            return area_list[i]
    }
}

但是后面摒弃了这种方式,选了另一种——构造key为area_id、值为该area_id所在的对象的objList对象:

let objList = {}
for (let i = 0; i < area_list.length; i++) {
    objList[area_list[i].area_id] = area_list[i]
}

至于选第二种的理由嘛,大家懂的~~

接下来就是处理方式了,咳咳~~废话不哆嗦,直接上——

let resultFunc = (name, area_id) => {
    let obj = objList[area_id]
    name = obj.name + name
    let id = obj.parent_id
    if (id) {
        return resultFunc(name, id)
    } else {
        return name
    }
}

for (let i = 0; i < location_list.length; i++) {
    let loc = location_list[i]
    loc.position = resultFunc(loc.position, loc.area_id)
}

这是一种处理方式......好吧我承认,上面这种方法是本人在写这篇文章的时候,为了凑点字数临时想的一种方法,不过感觉还行~

实际上我当时写的是这一种——

for (let i = 0; i < area_list.length; i++) {
    let obj = area_list[i]
    let Pid = obj.parent_id
    let Aid = obj.area_id
    if (Pid) 
    objList[Aid].name = objList[Pid].name + objList[Aid].name
}

此时objList是这样:

1558258715519 ok! 到了这一步,大家肯定都知道接下来怎么处理了,就到这里吧~~(话说怎么感觉自己当初写的方法还不如临时想的...... -_-)

134A0B915DB94338CB7E2FD57FA4579E

分享
点赞2
打赏
上一篇:代理工具Fiddler -调试与替换接口状态
下一篇:改造百度ueditor