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

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

如何使用字符串索引訪問 TypeScript 中的對象值

如何使用字符串索引訪問 TypeScript 中的對象值

慕桂英546537 2022-07-21 21:30:29
我有一個對象,我想使用字符串索引訪問對象值,但是當我嘗試在 TypeScript 中執行此操作時,我遇到了錯誤。// Declared Globallyconst Orientation = {    value: () => 0,    'next_value': () => someFunction.anotherFunction.sqrt(),};// _textValue type declared _textValue : string;// Declared inside constructorthis._textValue = 'next_value';// value used inside a function_getValue(){    const newValue = Orientation[this._textValue](); }在我使用的地方Orientation[this._textValue]();,我得到的錯誤是:元素隱式具有“any”類型,因為“any”類型的表達式不能用于索引類型“{ value: () => number; 'next_value': () => 數字;}'.ts(7053)關于如何解決這個問題的任何想法?
查看完整描述

1 回答

?
慕森卡

TA貢獻1806條經驗 獲得超8個贊

main.ts


export {}


interface IDictionary {

    [index: string]: string;

}

var params = {} as IDictionary;


//array with string index 

params = {a: "Bhavesh",b: "Bond", c: "Urmil"};


//get array data using string index

var getData = params['b']; // output: Bond

var getData = params['B']; // output: undefined


console.log(getData);

我認為您的問題將在上面的示例代碼中得到解決。謝謝你..!



查看完整回答
反對 回復 2022-07-21
  • 1 回答
  • 0 關注
  • 177 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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