1.未处理前的原数据
2.处理后的数据
[ {"label": "培训教室","value": "教学"},
{"label": "考试大厅", "value": "考试" },
{"label": "共享自习室", "value": "培训" },
{"label": "图书馆X", "value": "教学" } ]
3.步骤一:将appointType、appointName筛选出来,放入tempList、tempList1数组
response.forEach((item) => {
tempList.push(item.appointName)
})
response.forEach((item) => {
tempList1.push(item.appointType)
})
console.log("tempList:", tempList)
console.log("tempList1:", tempList1)
4.步骤二:将tempList、tempList1数组,转换成obj、obj1对象数组
let obj = {}
for (let key in tempList) {
obj[key] = tempList[key]
}
let obj1 = {}
for (let key in tempList1) {
obj1[key] = tempList1[key]
}
console.log("obj:", obj)
console.log("obj1:", obj1)
5.步骤三:将obj、obj1对象数组,合并成newObj1对象数组,并且赋予新的属性名label
let newObj1 = Object.keys((obj, obj1)).map((val, val1) => ({
label: obj[val],
}))
6.步骤四:将appointType属性内容的英文,转换成value属性内容的中文,在上面基础上添加。核心就是多条三元表达式判断,普通ifelse语句,语法上不通过,妈的!也是犯病了。
let newObj1 = Object.keys((obj, obj1)).map((val, val1) => ({
label: obj[val],
value: obj1[val1] == "TEACH" ? "教学"
: obj1[val1] == "EXAM" ? "考试"
: obj1[val1] == "DRILL" ? "训练"
: obj1[val1] == "TRAIN" ? "培训"
: obj1[val1] == "MINUTE" ? "会议"
: obj1[val1] == "STUDY" ? "自习"
: obj1[val1] == "OTHER" ? "其他"
: "未定义",
}))
// 下拉列表,赋值,打印
this.appointmentUnitRecord_options1 = newObj1
console.log("this.appointmentUnitRecord_options1", this.appointmentUnitRecord_options1)
7.完整JS数据处理代码展示
handleAppointmentUnitRecordAdd() {
this.reset_appointmentUnitRecord_form()
this.appointmentUnitRecord_open = true
this.appointmentUnitRecord_title = '新增'
let tempList = []
let tempList1 = []
get_appointmentUnit_names().then(response => {
response.forEach((item) => {
tempList.push(item.appointName)
})
response.forEach((item) => {
tempList1.push(item.appointType)
})
console.log("tempList:", tempList)
console.log("tempList1:", tempList1)
let obj = {}
for (let key in tempList) {
obj[key] = tempList[key]
}
let obj1 = {}
for (let key in tempList1) {
obj1[key] = tempList1[key]
}
console.log("obj:", obj)
console.log("obj1:", obj1)
let newObj1 = Object.keys((obj, obj1)).map((val, val1) => ({
label: obj[val],
value: obj1[val1] == "TEACH" ? "教学"
: obj1[val1] == "EXAM" ? "考试"
: obj1[val1] == "DRILL" ? "训练"
: obj1[val1] == "TRAIN" ? "培训"
: obj1[val1] == "MINUTE" ? "会议"
: obj1[val1] == "STUDY" ? "自习"
: obj1[val1] == "OTHER" ? "其他"
: "未定义",
}))
// 新增,下拉列表
this.appointmentUnitRecord_options1 = newObj1
console.log("this.appointmentUnitRecord_options1", this.appointmentUnitRecord_options1)
})
},