gpt4 book ai didi

javascript - 嵌套的 javascript 对象

转载 作者:行者123 更新时间:2023-11-30 10:19:52 24 4
gpt4 key购买 nike

我有一些像这样的对象:(wordpress 附件对象)

attachment.sizes (which is an object and contains:) 
.full (each contains height and width:)
.height
.width
.medium
.height
.width
.thumbnail
.height
.width

然后我有另一个对象:

props.size (which is either "full","medium" or "thumbnail")

我可以用某种方式写类似

attachment.sizes.(props.size).height 

获取高度? (我试过的时候没用)

最佳答案

是的,使用 []:

attachment.sizes[props.size].height 

a.ba['b'] 这两种表示法是等效的,但只有 [] 允许您将变量名用作属性(property)。

关于javascript - 嵌套的 javascript 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21882036/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com