首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从JSON对象读取嵌套映射时如何使用全局值

从JSON对象读取嵌套映射时如何使用全局值
EN

Stack Overflow用户
提问于 2020-07-10 14:10:30
回答 1查看 30关注 0票数 0

你好,我正在读这样一个JSON响应对象。

代码语言:javascript
复制
<td className="text-right flex">
     {finance.statuses.map((statuses) => {
     return statuses.currencyAmounts.map((amounts) => (
        <span className="pr-8" key={amounts.currencyId}>{amounts.maxGambleAmount}</span>
     ));
 })}
</td>

它工作得很好,但是我需要用索引amounts.currencyId,绑定每个值后面的全局变量数组中的货币缩写--我的问题是不能访问内部映射中的那些货币。

我尝试过类似的currenciesamounts.currencyId,但没有成功。

有人能帮我吗?谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-10 15:34:48

根据您的注释,currencies变量存在于您的mapStateToProps函数中。如果有一个mapStateToProps,很可能您将使用redux库。如果是这样的话,我强烈建议你花点时间熟悉它。

--但回到问题--,通常mapStateToProps会返回一个对象,并且这些对象的所有属性都应该在组件的props中可用。如果在您的currencies函数中已经有了可用的mapStateToProps,您可以简单地将它添加到return语句中,如下所示:

代码语言:javascript
复制
function mapStateToProps(state) {
    return {
        // this should make your "currencies" object/map available in the props of your component:
        currencies: state.currencies,
    };
}

// In your component you should have the "props" available as the parameter,
// you just have to access it now:
<td className="text-right flex">
     {finance.statuses.map((statuses) => {
     return statuses.currencyAmounts.map((amounts) => (
        <span className="pr-8" key={props.currencies[amounts.currencyId]}>{amounts.maxGambleAmount}</span>
     ));
 })}
</td>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62836141

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档