亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在對象中設置鍵名和文件名相同?

如何在對象中設置鍵名和文件名相同?

慕仙森 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 關注
  • 86 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號