【问题标题】:Iterate over all Array indexes within array with map method Javascript使用map方法Javascript遍历数组中的所有数组索引
【发布时间】:2019-09-10 02:00:46
【问题描述】:

已经为此苦苦挣扎了一段时间,如果你们有任何见解,将不胜感激。

这就是我的 JSON 数据的样子..

BinsByDayByOrchardsQCs [数组] > BinsByDayByOrchardsQCsDefects [数组]

我需要在网格表上显示每个“缺陷”,该表位于 BinsByDayByOrchardsQCsDefects 数组中,类似于

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

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

【问题讨论】:

  • 你能做一个 StackBlitz 或 Codepen 的例子来检查代码吗?另一方面,如果您想iterate 不仅在 0 上,您可以执行 `this.state.rows.map((qc, index) => allothercode)。干杯,西格弗里德。

标签: javascript arrays reactjs jsx


【解决方案1】:

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

但你也可以使用嵌套的maps

{
    this.state.rows.map((qc) =>
        qc.BinsByDayByOrchardsQCs.map((qc2) =>
            qc2.BinsByDayByOrchardsQCsDefects.map((qc3) =>
                <div className="row table">
                    {qc3.Defect}
                </div>
            )
        )
    )
}

【讨论】:

  • 对不起,我的男人回复晚了,但这工作得很好......干杯!
猜你喜欢
  • 2010-09-13
  • 1970-01-01
  • 1970-01-01
  • 2021-10-03
  • 1970-01-01
  • 1970-01-01
  • 2017-04-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多