数据格式化

70 阅读1分钟
let farNode = this.authorTag.filter(v=>v.pid === 0)\
           let sonNode = this.authorTag.filter(v=>v.pid !== 0)\
           console.log('aa',farNode,sonNode)\
           const son_map = sonNode.reduce((mp,data)=>{\
                mp[data.pid] = mp[data.pid] || []\
                mp[data.pid].push({value:data.tn,label:data.id})\
                return mp\
           },{})\
           console.log('aa',son_map)\
           const far_map = farNode.reduce((mp,data)=>{\
               mp.push({\
                   value:data.tn,\
                   label:data.id,\
                   children:son_map[data.id]||[]\
               })\
               return mp\
           },[])
// 获取城市\
    getCityData () {\
        const cache = localStorage.getItem('cityOptions')\
        if (cache) {\
            this.formatCityData = JSON.parse(cache)\
            this.cityData = this.handletree(this.formatCityData.children)\
        } else {\
            const url = this.service.getCityDate\
            this.$http.post(url, {}).then(res => {\
                if (res.retCode === 0) {\
                    this.formatCityData = res.body\
                    this.cityData = this.handletree(res.body.children)\
                    localStorage.setItem('cityOptions'JSON.stringify(this.formatCityData))\
                } else {\
                    this.messageError(res.message || '数据获取失败')\
                }\
            }).catch(e => {\
                console.log('e', e)\
                this.messageError('数据获取失败')\
            })\
        }\
    },\
    handletree (target) {\
        const result = target.map(i => {\
            const obj = {\
                value: i.id,\
                label: i.name\
            }\
            if (i['children'] && i['children'].length !== 0) {\
                obj.children = this.handletree(i.children)\
            }\
            return obj\
        })\
        return result\
    },