下面是对象,我的要求是,文件名和它的值应该相同。我们怎么能做到这一点?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
提交
取消