使用扩展语法尝试附加对象时遇到麻烦。
根据NewPerson是否在私人/专业场合出现的事实,我想向对象/数组附加其他键/值。
以某种方式不起作用。希望有人可以帮助我。 :(
var NewPerson = [
Firstname: this.state.addPersonFirstname,
Lastname: this.state.addPersonLastname,
Birthday: this.state.addPersonBirthday,
Occasion: this.state.addPersonOccasion,
];
if (this.state.addPersonOccasion === 'OccasionProfessional') {
NewPerson = [
...NewPerson,
...[ProfEmployerName: this.state.addPersonOccasionProfEmployerName],
...[ProfEmployerPLZ: this.state.addPersonOccasionProfEmployerPLZ],
...[ProfEmployerCity: this.state.addPersonOccasionProfEmployerCity],
...[ProfEmployerUVT: this.state.addPersonOccasionProfEmployerUVT]
]
}
if (this.state.addPersonOccasion === 'OccasionPrivate') {
NewPerson = [
...NewPerson,
...[PrivPersonStreet: this.state.addPersonOccasionPrivPersonStreet],
...[PrivPersonPLZ: this.state.addPersonOccasionPrivPersonPLZ],
...[PrivPersonCity: this.state.addPersonOccasionPrivPersonCity]
]
}
var CombinedPersons
if (PreviousPersons === null) {
CombinedPersons = NewPerson
} else {
CombinedPersons = [...PreviousPersons, ...NewPerson]
}
答案 0 :(得分:1)
您应该使用对象而不是数组,因为对象具有键值对。您可以做(使用ES6语法):
tx.commands.requireSingleCommand
答案 1 :(得分:0)
您不需要传播新属性...
您可以:
if (this.state.addPersonOccasion === 'OccasionProfessional') {
NewPerson = {
...NewPerson,
ProfEmployerName: this.state.addPersonOccasionProfEmployerName,
ProfEmployerPLZ: this.state.addPersonOccasionProfEmployerPLZ,
//... and so on
}
}
答案 2 :(得分:0)
在这种情况下,您似乎混淆了数组和对象。您需要将一个人的所有属性隔离到一个实体中。 Object
在这种情况下效果最佳。
var NewPerson = {
Firstname: this.state.addPersonFirstname,
Lastname: this.state.addPersonLastname,
Birthday: this.state.addPersonBirthday,
Occasion: this.state.addPersonOccasion,
};
if (this.state.addPersonOccasion === 'OccasionProfessional') {
NewPerson = {
...NewPerson,
ProfEmployerName: this.state.addPersonOccasionProfEmployerName,
ProfEmployerPLZ: this.state.addPersonOccasionProfEmployerPLZ,
ProfEmployerCity: this.state.addPersonOccasionProfEmployerCity,
ProfEmployerUVT: this.state.addPersonOccasionProfEmployerUVT
}
}
if (this.state.addPersonOccasion === 'OccasionPrivate') {
NewPerson = {
...NewPerson,
PrivPersonStreet: this.state.addPersonOccasionPrivPersonStreet,
PrivPersonPLZ: this.state.addPersonOccasionPrivPersonPLZ,
PrivPersonCity: this.state.addPersonOccasionPrivPersonCity
]
}
var CombinedPersons
if (PreviousPersons === null) {
CombinedPersons = [NewPerson]
} else {
CombinedPersons = [...PreviousPersons, {...NewPerson}]
}
PreviousPersons
将是一个人对象数组。
答案 3 :(得分:0)
将所有答案组合成最终版本:
var NewPerson = {
Firstname: this.state.addPersonFirstname,
Lastname: this.state.addPersonLastname,
Birthday: this.state.addPersonBirthday,
SigImage: this.sigPad.getCanvas().toDataURL('image/png'),
Occasion: this.state.addPersonOccasion,
};
if (this.state.addPersonOccasion === 'OccasionProfessional') {
NewPerson = {
...NewPerson,
ProfEmployerName: this.state.addPersonOccasionProfEmployerName,
ProfEmployerPLZ: this.state.addPersonOccasionProfEmployerPLZ,
ProfEmployerCity: this.state.addPersonOccasionProfEmployerCity,
ProfEmployerUVT: this.state.addPersonOccasionProfEmployerUVT
}
}
if (this.state.addPersonOccasion === 'OccasionPrivate') {
NewPerson = {
...NewPerson,
PrivPersonStreet: this.state.addPersonOccasionPrivPersonStreet,
PrivPersonPLZ: this.state.addPersonOccasionPrivPersonPLZ,
PrivPersonCity: this.state.addPersonOccasionPrivPersonCity
}
}
// Save the user input to NewPerson var - End
// Create combined var with PreviousPersons and NewPerson - Start
var CombinedPersons
if (PreviousPersons === null) {
CombinedPersons = [NewPerson]
} else {
CombinedPersons = [ ...PreviousPersons, NewPerson ]
}
// Create combined var with PreviousPersons and NewPerson - End