const objData = [{ type: '人类', name: '夏目', age: 17 }, { type: '妖怪', nickName: '猫咪老师', level: '高级' },]
//提前定义好form表单格式
const human = {
name: '',
age: ''
}
const cuteAnimal = {
nickName: '',
level: ''
}
for (let key in objData) {
switch (objData[key].type) {
case '人类':
//遍历对象 赋值给form表单
for (const keyv in objData[key]) {
human[keyv] = objData[key][keyv]
}
break
default:
//cuteAnimal = objData[key]
for (const keyv in objData[key]) {
cuteAnimal[keyv] = objData[key][keyv]
}
break
}
}
console.log(JSON.stringify(human)) //{"name":"夏目","age":17,"type":"人类"}
console.log(JSON.stringify(cuteAnimal)) //{"nickName":"猫咪老师","level":"高级","type":"妖怪"}
网友评论