gpt4 book ai didi

how to loop through in React JSX and build multiple based on condition(如何在Reaction JSX中循环并基于条件构建多个)

转载 作者:bug小助手 更新时间:2023-10-24 21:31:57 27 4
gpt4 key购买 nike



I need to divide data in JSON in multiple columns. I have the column header length as variable: colspanL1 . In the JSON I am getting an array which

我需要将JSON中的数据分成多列。我将列标题长度作为变量:colspanL1。在JSON中,我得到一个数组,它



  1. sometime gets data for all the columns

  2. Sometime gets data for some columns and for others get N/A

  3. sometime gets data just for some columns


The below code is able to handle first 2 conditions. For last condition I want to run a for loop for the below piece <td> {data === "N/A" ? "" : getFormattedValue(data, true)} </td> I want this section to be inside FOR loop which will run for the missing data. This FOR loop should run based on condition:

下面的代码能够处理前两种情况。对于最后一个条件,我想为下面的{data=“N/A”?“”:getFormattedValue(data,true)}运行一个for循环,它将运行丢失的数据。此for循环应根据以下条件运行:


(cellLen -1 === index && cellLen !== colspanL1 )

(cell Len-1=索引&&cell Len!==colspanL1)


where cellLen is the length of the Array and index is index of items inside array. That means when (cellLen -1)= Index (end of the array) but cellLen is not equal to the number of columns. At that time the FOR loop should run to have those many empty cells i.e. "<td>" tag. Below is the TBODY code where I want to introduce this FOR logic

其中cell Len是数组的长度,index是数组中项的索引。这意味着当(cell Len-1)=Index(数组末尾),但cell Len不等于列数。那时,for循环应该运行以拥有许多空单元格,即“”标记。下面是我想要在其中引入这一逻辑的TBODY代码




<tbody>
{Object.entries(formattedData.rowsData).map( ([key, rowData], index) => { return ( rowData && Array.isArray(rowData) && (
<tr>
{rowData.map((cellData, index) => { return index === 0 ? (
<td>{cellData}</td>
) : ( cellData.map((data, index) => { return typeof data === "string" && data.includes("%") ? (
<td className={`${ parseFloat(data)> 0 ? "color-green" : "color-red" }`} > {data}
</td>
) : (
<td>
{data === "N/A" ? "" : getFormattedValue(data, true)}
</td>
); }) ); })}
</tr>
) ); } )}
</tbody>




I need to get something like this:

我需要这样的东西:




<tbody>
{Object.entries(formattedData.rowsData).map( ([key, rowData], index) => { return ( rowData && Array.isArray(rowData) && (
<tr>
{rowData.map((cellData, index) => { return index === 0 ? (
<td>{cellData}</td>
) : ( cellData.map((data, index) => { return typeof data === "string" && data.includes("%") ? (
<td className={`${ parseFloat(data)> 0 ? "color-green" : "color-red" }`} > {data}
</td>
) : ///this is the code change I am looking for.IF condition satisfied run FOR to create TD
IF (cellLen -1 === index && cellLen !== colspanL1 ) then for(i=0;i<=colspanL1 - cellLen;i++)
(
<td>
{data === "N/A" ? "" : getFormattedValue(data, true)}
</td>
); }) ); })}
</tr>
) ); } )}
</tbody>




更多回答
优秀答案推荐
更多回答

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com