1

I'm new in React and I stuck with this. Suppose I have a state like this

state = {
    dataSource: {
      model: ["Slip on", "Running", "Sneaker"],
      colors: ["Dark", "Light", "Colorful"],
      activity: ["School", "Hang out", "Rest"],
    }
};

I want to render a table with the header as the name of the object inside dataSource and value correspond to that object. I already tried using map() and cause I knew that map() can not be used on object I tried to change the state like this

state = {
    dataSource: [
      ["Slip on", "Running", "Sneaker"],
      ["Dark", "Light", "Colorful"],
      ["School", "Hang out", "Rest"],
    ]
};

then try to solve it like this

render() {
  <table>
    <tbody>
      {this.state.dataSource.map((c) => (
        <tr>
          {c.map((x) => (
            <td>{x}</td>
          ))}
        </tr>
      ))}
    </tbody>
  </table>
}

it did render the value but not the right way, so I wonder if there is a way to do it? Thanks for your help :)

3
  • 1
    Please just show what you tried to do Commented Jun 13, 2020 at 11:37
  • state.dataSource vs state.dataSet what is the one? Commented Jun 13, 2020 at 12:03
  • oh sorry i mean state.dataSource. i will edit right away Commented Jun 13, 2020 at 12:10

2 Answers 2

1

Object.keys might help

const { Component } = React;

class App extends Component {

  constructor(props) {
    super(props);
    
    this.state = {
      dataSource: {
        model: ["Slip on", "Running", "Sneaker"],
        colors: ["Dark", "Light", "Colorful"],
        activity: ["School", "Hang out", "Rest"],
      }
    };
  }

  render() {
    const { dataSource } = this.state;
    const arr = Array(dataSource.model.length).fill(0);
    
    return <table>
      <thead>
        <tr>
          {Object.keys(dataSource).map(pr => <th key={pr}>{pr}</th>)}
        </tr>
      </thead>
      <tbody>
        {arr.map((pr, index) => <tr key={index}>
          {Object.keys(dataSource).map(key => <td key={key}>{dataSource[key][index]}</td>)}
        </tr>)}
      </tbody>
    </table>
  }
}

ReactDOM.render(
    <App />,
    document.getElementById('root')
  );
<script src="https://unpkg.com/react/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
<div id="root"></div>

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

Comments

0

Map can not work. In case of objects you should use objects methods, for example something like

return
  Object.entries(state.dataSource).map(([key: tableName, value]) => 
    <div>
      <h1>{tableName}</h1>
      {value.map(v => <div>{v}</div>}
    </div>
  )

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.