第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

javascript如何動(dòng)態(tài)地將子字典附加到父字典

javascript如何動(dòng)態(tài)地將子字典附加到父字典

慕工程0101907 2022-05-22 18:01:25
我有一個(gè)像這樣的Javascript對(duì)象:const childDict = {  address: {    zip: "GGHG654",    city: "Morocco",    number: 40  }}我想在這樣的循環(huán)中動(dòng)態(tài)地將它添加到另一個(gè)父字典中:let parentDict = {}for(let i = 0 ; i < 3; i++){  parentDict["place" + i] = childDict}所以最后我得到了一個(gè)像這樣的字典:{  place0: {    address: {      zip: "GGHG654",      city: "Morocco",      number: 40    }  },  place1: {    address: {      zip: "GGHG654",      city: "Morocco",      number: 40    }  }}然而,for循環(huán)給了我一個(gè)編譯錯(cuò)誤:Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'.  No index signature with a parameter of type 'string' was found on type '{}'.
查看完整描述

3 回答

?
九州編程

TA貢獻(xiàn)1785條經(jīng)驗(yàn) 獲得超4個(gè)贊

let parentDict = {}

這沒(méi)有明確設(shè)置導(dǎo)致此問(wèn)題的類型。嘗試提供any如下類型:

let parentDict:any = {}

或者,更準(zhǔn)確地說(shuō):

let parentDict:{[key: string]: object} = {}


查看完整回答
反對(duì) 回復(fù) 2022-05-22
?
Smart貓小萌

TA貢獻(xiàn)1911條經(jīng)驗(yàn) 獲得超7個(gè)贊

您只需向父字典添加適當(dāng)?shù)慕涌?,因?yàn)榇蜃指鍟?huì)根據(jù)初始值自動(dòng)分配類型,初始值沒(méi)有任何鍵


interface IParentDict {

    [key: string]: any; // possibly change any to the typeof child dict

}

const parentDict: IParentDict = {};


查看完整回答
反對(duì) 回復(fù) 2022-05-22
?
慕桂英546537

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超10個(gè)贊

像這樣試試


let parentDict: any = {};


另一種選擇可能是以更正確的方式指定類型,例如


let parentDict: {[key: string]: any} = {};


另一種駭人聽(tīng)聞的方式是


let parentDict = {}


for(let i = 0 ; i < 3; i++){

    (parentDict as any)["place" + i] = childDict

}


查看完整回答
反對(duì) 回復(fù) 2022-05-22
  • 3 回答
  • 0 關(guān)注
  • 151 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)