10

求大佬们优化一下代码,屎山代码给孩子头皮想破了

 3 years ago
source link: https://www.v2ex.com/t/784284
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

V2EX  ›  Vue.js

求大佬们优化一下代码,屎山代码给孩子头皮想破了

  happyCodings · 1 天前 · 4510 次点击
秉着不写垃圾代码的原则,求大佬优化一下,最优值奉上红包
需求是这样的:

万恶后端传过来的数据:
"data":[{"province":"上海市","city":"上海市市辖区","adcode":"310118","district":"青浦区"},
{"province":"江苏","city":"苏州市","adcode":"320506","district":"吴中区"},
{"province":"山西省","city":"阳泉市","adcode":"140302","district":"城区"},
{"province":"上海市","city":"上海市市辖区","adcode":"310101","district":"黄浦区"},
{"province":"河北省","city":"石家庄市","adcode":"130123","district":"正定县"}]

需要整合成这样 elementUi 中级联选择器中的数据:
(只要有相同的就要去重并在之前的 children 下加入重复的 children )
addressOptions: [
{
value: "zhinan",
label: "指南",
children: [
{
value: "shejiyuanze",
label: "设计原则",
children: [
{
value: "yizhi",
label: "一致",
},
{
value: "fankui",
label: "反馈",
},
{
value: "xiaolv",
label: "效率",
},
{
value: "kekong",
label: "可控",
},
],
},
{
value: "daohang",
label: "导航",
children: [
{
value: "cexiangdaohang",
label: "侧向导航",
},
{
value: "dingbudaohang",
label: "顶部导航",
},
],
},
],
},
],

我的垃圾代码:

dealWithAddressList(arr, name) {
var city = {};
return arr.reduce(function (item, ele) {
console.log(ele[name]);
if (city[ele[name]]) {
item.forEach((eles) => {
if (eles.label == ele[name]) {
eles.children.forEach((eless) => {
if (eless.value == ele.city) {
eless.children.push({
value: ele.adcode,
label: ele.district,
});
} else {
eles.children.push({
value: ele.city,
label: ele.city,
children: [
{
value: ele.adcode,
label: ele.district,
},
],
});
}
});
}
});
console.log(city[ele[name]]);
} else {
city[ele[name]] =
true &&
item.push({
value: ele.province,
label: ele.province,
children: [
{
value: ele.city,
label: ele.city,
children: [
{
value: ele.adcode,
label: ele.district,
},
],
},
],
});
}
console.log(item);
return item;
}, []);
},

求大佬优化一下,可以的话有红包奉上

Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK