使用map方法Javascript迭代数组中的所有Array索引

问题描述 投票:1回答:1

一段时间以来一直在努力解决这个问题,并希望你们有任何见解。

这就是我的JSON数据看起来像.enter image description here

BinsByDayByOrchardsQCs [Array]> BinsByDayByOrchardsQCsDefects [Array]

我需要在网格表上显示每个“缺陷”,它位于BinsByDayByOrchardsQCsDefects数组中,看起来像这样的enter image description here

到目前为止我所拥有的是{this.state.rows.map((qc) => <div className="row table">{qc.BinsByDayByOrchardsQCs[0].BinsByDayByOrchardsQCsDefects[0].Defect}</div>

这个目前返回Hail Damage,Sunburn,Sunburn,Sunburn,我的问题是你如何从整个阵列返回所有缺陷而不仅仅是[0]

javascript arrays reactjs jsx
1个回答
3
投票

我喜欢使用Lodash进行收集操作。

但你也可以使用嵌套的maps

{
    this.state.rows.map((qc) =>
        qc.BinsByDayByOrchardsQCs.map((qc2) =>
            qc2.BinsByDayByOrchardsQCsDefects.map((qc3) =>
                <div className="row table">
                    {qc3.Defect}
                </div>
            )
        )
    )
}
© www.soinside.com 2019 - 2024. All rights reserved.