0

I am trying to generate 2 columns in each row of a table from an array in JSX. In Javascript it would be simple but JSX needs the opening and closing tag in the same area.

This is what I would do in Javascript:

var tTable = '<table>';

var newArray = ['2', '3', '4', '5', '6'];
var newTr = '';

for (var i = 0; i < newArray.length; i++) {
  if (i % 2 == 0) {
    newTr += (i > 0) ? '</tr><tr>' : '<tr>'
  }

  newTr += '<td>' + newArray[i] + '</td>';
}

newTr += '</tr>';
tTable += newTr + '</table>';
document.write(tTable);

This will produce an array as follows:

2 3
4 5
6 

2 Answers 2

1

This should help :

showTable() {
    const tableRows = [];
    for (let i = 0; i < newArray.length; i = i + 2) {
          tableRows.push(
            <tr key={newArray[i] + "-" + newArray[i + 1] + "-" + i}>
                <td style={{ border: "1px solid black " }}>
                    {newArray[i]}
                </td>
                <td style={{ border: "1px solid black " }}>
                    {newArray[i + 1]}
                </td>
            </tr>
          );
    }
    return tableRows;
}



render() {
    return (
      <table style={{ border: "1px solid black " }}>
        <tbody>
          <th style={{ border: "1px solid black " }}>column1</th>
          <th style={{ border: "1px solid black " }}>column2</th>
        </tbody>
        <tbody>{this.showTable()}</tbody>
      </table>
    );
  }
}

Working solution :

Edit react-konva-basic-demo

Sign up to request clarification or add additional context in comments.

3 Comments

Is there a way to test this on something like JSFiddle or JSbin?
Thank you! Now that I see it I updated the question to be more clear. Your solution produces table with 2 columns but just replicates each array value in each column, I would like to have 2 columns but each column has a different array value.
@Whitecat i have updated the solution. Please check if this is what you were looking for. Right now i couldn't think of anything better than using a loop for this. If i find something, i will surely update here. see if it works for you
0

You could try the following:

render() {
    return 
        <table>
            <tbody>
                {
                    newArray.map((entry, index) => {
                         if (index === newArray.length - 1) {
                            return <tr><td>entry</td></tr>
                         }
                         return (
                             <tr>entry</tr>
                             <tr/>
                         )
                    })
                }
            </tbody>
        </table>
    )
}

I know React complains if you dont nest your <tr>s in a <tbody>. Hopefully that should give you what you want. The translation to a functional component case should be trivial.

Hope that helps.

3 Comments

can you put the html in back ticks like these: ` that way it shows up.
Sorry for the confusion. I meant the inline html code not the actual code itself.
I think this has some issues like an extra closing tag "<tr/>" could you make it work on codesandbox.io or somewhere else? like the answer by @Arpit Agrawal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.