为了账号安全,请及时绑定邮箱和手机立即绑定

如何在对象中设置键名和文件名相同?

如何在对象中设置键名和文件名相同?

慕仙森 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

        }

      }

    };

希望这可以帮助。


查看完整回答
反对 回复 2022-10-27
  • 1 回答
  • 0 关注
  • 52 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信