使用场景
el-cascader层级数据,将后台字段key更改为label,value,children
<el-cascader
placeholder=""
:options="relationList"
:show-all-levels="false"
filterable
></el-cascader>
方法一:递归更改或者新增key
relationListFn () {
api.regionOrganize.districtList().then(res => {
this.relationList = res.data
this.rewriteKey(this.relationList)
},
rewriteKey (val) {
val.forEach(item => {
item.label = item.bizName
item.value = item.bizCode
item.children = item.childrenList
if (item.children) {
this.rewriteKey(item.children)
}
})
},
方法二:操作字符,将数组stringfy后,替换key,后转为数组
relationListFn () {
api.regionOrganize.districtList().then(res => {
this.relationList = res.data
const value = this.relationList && JSON.stringify(this.relationList).replace(/bizCode/g, 'value')
const label = value && value.replace(/bizName/g, 'label')
const children = label && label.replace(/childrenList/g, 'children')
this.relationList = children && JSON.parse(children)
})
}