【发布时间】:2020-03-18 09:57:03
【问题描述】:
我有一个像这样的 Javascript 对象:
const childDict = {
address: {
zip: "GGHG654",
city: "Morocco",
number: 40
}
}
我想像这样在循环中动态地将它添加到另一个父字典中:
let parentDict = {}
for(let i = 0 ; i < 3; i++){
parentDict["place" + i] = childDict
}
所以最后我得到了一个这样的字典:
{
place0: {
address: {
zip: "GGHG654",
city: "Morocco",
number: 40
}
},
place1: {
address: {
zip: "GGHG654",
city: "Morocco",
number: 40
}
}
}
然而,for 循环给了我一个编译错误:
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 '{}'.
【问题讨论】:
-
当前代码有什么问题?
-
@palaѕн parentDict["place" + i] 给出编译错误。我编辑了问题以包含错误。
-
只循环到 childDict 项目的长度
标签: javascript typescript dictionary javascript-objects