慕仙森
2022-10-27 16:02:01
下面是對象,我的要求是,文件名和它的值應該相同。我們怎么能做到這一點?const obj = { email: this.formUser.value.email, phoneNumber: this.formUser.value.mobile, profileImageName: file, fileObj: { // want the same logo.png to be the key name here file: { fileName: file, //value is logo.png fileDescription: this.formUser.value.fileDescription } }}
1 回答

慕桂英3389331
TA貢獻2036條經驗 獲得超8個贊
[]
可用于將計算值定義為 JSON 對象中的屬性名稱。有關更多信息,請參閱此處
下面是一個相同的例子
let myKey = "logo.png"
let val = "Logo.png"
var obj = {
[myKey]: val,
}
console.log(obj)
所以在你的情況下,這可以像下面這樣完成
const obj = {
email: this.formUser.value.email,
phoneNumber: this.formUser.value.mobile,
profileImageName: file,
[file]: {// using [] will allow to use dynamic key names in json obj
file: {
fileName: file, //value is logo.png
fileDescription: this.formUser.value.fileDescription
}
}
};
希望這可以幫助。
添加回答
舉報
0/150
提交
取消