【问题标题】:Can't understand why I only get one table row in this react loop不明白为什么我在这个反应循环中只得到一个表格行
【发布时间】:2020-06-21 13:26:36
【问题描述】:

Tables 变量是一个包含对象数组的对象 const Tables = {QD1:[{key:value, key:value, key:value, key:value}],QD1:[{key:value, key:value, key:value,key:value}]}

我正在尝试根据它的内容构建一个表。但是,当我运行以下代码时,当我期望为数组中的每个对象获取一行时,我只得到一行。谁能告诉我我做错了什么?

    render() {
const Tables = this.state.tableResult.Tables;
console.log(Tables);
if(typeof Tables !== 'undefined'){
  const display = Object.keys(Tables).map((bannerRowKey, bannerRowIndex) => {  
    let crossTab = Object.values(Tables[bannerRowKey]);
    console.log(crossTab);
    // if(bannerRowIndex===0){
    const TableRow = crossTab.map((crossTabRow,crossTabRowIndex)  => (
      // console.log(crossTabRowIndex);
      <tr key={'header'+crossTabRowIndex.toString()}></tr>
    ));
    return (
    <thead key={'header'+bannerRowIndex.toString()}>{TableRow}</thead>
    )

  })


  return <div id="table_holder" className="table-responsive mx-3">
  <table className="table-sm">
  {display}
  </table>
</div>
}else{
  return (
  <div id="table_holder" className="table-responsive mx-3">
    <table className="table-sm">
    </table>
  </div>
  );
}

}

【问题讨论】:

  • TableRow 中,您没有显示任何内容。在 中添加类似这样的内容:&lt;td&gt;..{display value of crossTab}&lt;/td&gt;
  • 虽然这并没有解决问题,但它确实帮助我调试它,以便我可以。感谢您回复我。

标签: reactjs react-map-gl


【解决方案1】:

我意识到我的地图返回了键和值,所以:QD1:[{key:value, key:value, key:value,key:value}] 而不是:[{key:value, key:value ,键:值,键:值}]。

【讨论】:

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签