1

Im creating a table from JSON data, and main problem is I can't use object keys to map it. My json something like this:

[{
    key: 'val',
    key2: 'val',
    key3: 'val'
},
{
    key: 'val',
    key2: 'val',
    key3: 'val'
}]

Here is how i implemented header with columns:

class Table extends Component {

    render() {
        const header = this.props.data.slice(0, 1);
        return (<table>
            <thead>
            <TableHead children={header}/>
            </thead>
            <tbody>
            <TableBody children={this.props.data}/>
            </tbody>
        </table>)
    }
    }

   export default Table;

   class TableHead extends Component {

    render() {
        return (
            <tr>
                {this.props.children.map((header) => {
                    return Object.keys(header).map((el) => {
                        return <th>{el}</th>
                    })
                })}
            </tr>
        )
     }
     }

     export default TableHead;

But I can't understand how to map my table body iterating over objects... I sliced my JSON for header, but I can't do this with data, and my table looks like this

class TableBody extends Component {

render() {
    const row = this.props.children.map((row) => {
        return Object.values(row).map((el,i) => {
            if (i%Object.values(row).length===0) {
                return <tr><td>{el}</td></tr>
            }else{
                return <td>{el}</td>
            }
        })
    });
    return (
        <tbody>
            {row}
        </tbody>
    )
}
}

export default TableBody;
3
  • Your query isn’t clear can you please describe your issue and expected output in detail Commented Feb 14, 2019 at 18:54
  • From your image the main problem is with TableBody so please post its code as well. Commented Feb 14, 2019 at 19:05
  • yes updated the post Commented Feb 14, 2019 at 19:17

1 Answer 1

2

I would extract the keys and re-use when mapping over the rows for the TableBody.

Something like

class Table extends Component {
  render() {
    const { data } = this.props;
    const columns = Object.keys(data[0]);
    return (
      <table>
        <thead>
          <TableHead columns={columns} />
        </thead>
        <tbody>
          <TableBody columns={columns} data={data} />
        </tbody>
      </table>
    );
  }
}

class TableHead extends Component {
  render() {
    const { columns } = this.props;
    return (
      <tr>
        {columns.map(header => {
          return <th>{header}</th>;
        })}
      </tr>
    );
  }
}

class TableBody extends Component {
  render() {
    const { columns, data } = this.props;
    return data.map(row => (
      <tr>
        {columns.map(cell => (
          <td>{row[cell]}</td>
        ))}
      </tr>
    ));
  }
}

Edit k6o7n4w7

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

Comments

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.