首页 > 解决方案 > 用选定的属性反应 .map

问题描述

我需要使用具有特定属性的地图显示元素。

我的代码显示所有项目对象的属性。

const {report, info} = this.state;
const max = this.state.info.map((info, index) =>
    <DonateReport key={index} title={info.title} opened={info.opened}
                  onClick={() => this.toggleInfo(index)}>
        {report.map(({item}) => ( //<--HERE
                <h2 >{item.donaters_count}</h2>
    ))}
    </DonateReport>
);

请告诉我,如何显示具有以下参数的属性:item.month == info.key 也许还有办法解决这个问题......

我真的很感激任何帮助。请不要发誓,我真的在第二天就坐在这个问题上

标签: javascriptarraysreactjsobject

解决方案


我已经使用ternary operator并格式化了您的 DonateReport 组件。它应该适合你。让我知道。

 <DonateReport key={index} title={info.title} opened={info.opened}
                        onClick={() => this.toggleInfo(index)}>
                        {
                            report.map(item => this.state.info.key === item.month ? //<--HERE
                            <h2 >{item.donaters_count}</h2>
                            : null)
                        }
                    </DonateReport>

推荐阅读