1、案例 接口返回的字典值,需求通过字典表数据回显对应的文字
1、 接口返回的数据
companyList: ["S", "D"]
2.字典表的数据
tee:[
{
children: null
id: 1
label: "制造业"
leaf: false
value: "C"
}
]
3、处理字典表的数据
const data = {};
tee.map((item) => {
data[item.value] = item.label;
if (item.children) {
for (let i = 0; i < item.children.length; ++i) {
let child = item.children[i];
data[child.value] = child.label;
}
}
});
4、把字典表的数据转化成对象 类似下面
{
A: "农、林、牧、渔业"
B: "采矿业"
C: "制造业"
}
5、拿到想要的数据, 把value值变成label值
this.companyList = this.companyList.map((item) => this.companys[item]);
2、案例 接口返回的字典表,需求把返回数据赋值到对应字段里面
1、接口返回数据
res:{
policy_coordination_dept: [
dictLabel: "裕华区人民政府"
dictType: "policy_coordination_dept"
dictValue: "01"
]
policy_industry_type: [
dictLabel: "裕华区人民政府"
dictType: "policy_industry_type"
dictValue: "01"
]
policy_level: [
dictLabel: "裕华区人民政府"
dictType: "policy_level"
dictValue: "01"
]
}
2、data对象里面字段
{
responsibleDeptDict: [],
redemptionTypeDict: [],
levelDict:[]
}
3、定义一个对象数据
const dicts = {
policy_coordination_dept: "responsibleDeptDict",
policy_industry_type: "redemptionTypeDict",
policy_level: "levelDict"
}
4、通过Object.keys方法转转对象,用map遍历给data里面的字段赋值
Object.keys(res).map((key)=>{
this[dicts[key]] = res[key]
})
5、最后处理好的数据是
responsibleDeptDict: [{
dictLabel: "裕华区人民政府"
dictType: "policy_coordination_dept"
dictValue: "01"
}],
redemptionTypeDict: [
dictLabel: "裕华区人民政府"
dictType: "policy_industry_type"
dictValue: "01"
]
levelDict: [
dictLabel: "裕华区人民政府"
dictType: "policy_level"
dictValue: "01"
]
这个是我在自己觉得不错的处理方法,有可能别人有更好的解决也欢迎大家留言,我也会备注文章里面的, 以后这个文章会不断更新上面,记得关注收藏哦!!!