微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用变量作为对象键的值 - 在 React-native 中

如何解决使用变量作为对象键的值 - 在 React-native 中

开发人员级别 - 我最近开始使用 JS/React/React-native 等编码语言...

项目语言 - React Native(带博览会)

问题 - 在调用对象中的值时如何提及变量。

代码-

我的 react-native 项目中有这个对象 -

export const collectionItemThemes = {
    themeColor: {
        character: "245,101,101",actor: "218,55,55",drink: "255,99,71",food: "56,161,105",object: "237,137,54",carBrand: "56,178,172",action: "90,103,216",emotion: "159,122,234",}
}

在主文件中,我获取存储在变量中的密钥,例如 -

const theme = "character"const theme = "food"

现在,我如何在使用上述对象中的相应值时调用此变量

type = collectionItemThemes.themeColor.**the variable goes here** 

尝试了以下 -

themeColor = collectionItemThemes.themeColor.type 
themeColor = collectionItemThemes.themeColor.[type]  

但是这些都行不通。

请有人帮助我了解这种情况下的语法。

解决方法

您已经接近了,移除 .。这是http.client.HTTPConnection

themeColor = collectionItemThemes.themeColor[type] 
,

我试过 {collectionItemThemes.themeColor[key] 并且效果很好。

您尝试了错误的 {collectionItemThemes.themeColor.[key]

这是我试过的零食; https://snack.expo.io/@irfanwani/obj

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。