-3

I need to render the below object into subsequent tr/tds via JSX...

    {
  "0": [
    {
      "colIndex": 0,
      "data": {
        "richTextModule": "Data row 1 cell 1"
      }
    },
    {
      "colIndex": 0,
      "data": {
        "richTextModule": "Data row 2 cell 1"
      }
    }
  ],
  "1": [
    {
      "colIndex": 1,
      "data": {
        "richTextModule": "Data row 1 cell 2"
      }
    },
    {
      "colIndex": 1,
      "data": {
        "richTextModule": "Data row 2 cell 2"
      }
    }
  ],
  "2": [
    {
      "colIndex": 2,
      "data": {
        "richTextModule": "Data row 1 cell 3"
      }
    },
    {
      "colIndex": 2,
      "data": {
        "richTextModule": "Data row 2 cell 3"
      }
    },
    {
      "colIndex": 2,
      "data": {
        "richTextModule": "Data row 3 cell 3"
      }
    }
  ]
}

I've tried

Object.values(obj).map((column, index) => {
    return column.map((row, rowIndex) => {
        return obj[index][rowIndex].data.richTextModule;
    });
}

Expected output should be...

    <tr>
        <td>Data row 1 cell 1</td>
        <td>Data row 1 cell 2</td>
        <td>Data row 1 cell 3</td>
    </tr>
    <tr>
        <td>Data row 2 cell 1</td>
        <td>Data row 2 cell 2</td>
        <td>Data row 2 cell 3</td>   
    </tr>

Any ideas on how to achieve this?

5
  • 2
    FYI: You're not returning anything from your Object.values().map() call. Commented Jul 26, 2022 at 21:19
  • @ChrisG Surely abstracting the data re-ordering out of the component rendering is a valid way to go about it no? I know how to render tables in React. Commented Jul 26, 2022 at 21:22
  • Sorry, I misread your question. Yes, you'll want to create a two dimensional array first. This has nothing to do with React btw (which is what confused me) Commented Jul 26, 2022 at 21:24
  • Yeah my mistake @ChrisG - ultimate goal is to render the resulting array into table in JSX but nothing to do with the data sorting I agree Commented Jul 26, 2022 at 21:27
  • Here's one way it should work: jsfiddle.net/0jh1Luad Commented Jul 26, 2022 at 21:48

1 Answer 1

0

Think what you're trying to do is:

<table>
{Object.values(obj).map((col, index) => <tr key={index}>{
      col.map((row, rowIndex) => <td key={rowIndex}>${obj[index][rowIndex].data.richTextModule}</td>)
    }</tr>
)}
</table>

or you could do:

<table>
  {Object.values(obj).map((col, index) => {
    return (
      <tr key={index}>
        {col.map((row, rowIndex) => {
          return <td key={rowIndex}>${obj[index][rowIndex].data.richTextModule}</td>
        })}
      </tr>
    )
  })}
</table>

You should return in a map, reference:

You also should add a key, reference:

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

2 Comments

The array has to be restructured because the inner arrays contain the cells of a column, not a row
ah I thought his issue was he couldnt get it to work and his code was wrong

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.