0

If I have a state like so:

 state = {
    Data: [
      {
          inputArr: [1, 2, 3],

      },
      {
         inputArr: [1, 2, 3],

      },
      {
          inputArr: [1, 2, 3],

      }
    ]
  };

and I want to make a table from the input Arr so that I will have 3 rows 3 cols and it will be in order, 123,123,123

I thought about double map but it doesn't work, is there a better way?

const test=this.state.data.map( (val)=>{
   val.map( (val2)=>{
 <tr>
    return (
  <th> val2 </th>
      )
    }
 </tr>
}
4
  • 1
    That should actually work fine. You're just not returning anything from your first map though, also val2 should be between curly brackets. Let me know if that works. Commented Sep 26, 2018 at 14:40
  • the <tr> above the return is giving me a syntax error.. Commented Sep 26, 2018 at 14:46
  • and if I remove the tr it sais val is not a function Commented Sep 26, 2018 at 14:48
  • You had to access val.inputArr instead of val, and also the opening of the tr tag should be outside the map. Try this: codesandbox.io/embed/q8zl5p9x5q Commented Sep 26, 2018 at 15:30

2 Answers 2

1

You should have a nested map and return JSX elements from both of them

const test= this.state.Data.map((val, index)=>{
     return (
        <tr key={index}>
           {val.inputArr.map((val2)=>{
                return (
                  <th key={val2}>{val2}</th>
                )
            })}
        </tr>
     )
}

You also need to provide a key for the elements

class App extends React.Component {
  state = {
    Data: [
      {
          inputArr: [1, 2, 3],

      },
      {
         inputArr: [1, 2, 3],

      },
      {
          inputArr: [1, 2, 3],

      }
    ]
  };

  render() {
    const test= this.state.Data.map((val, index)=>{
         return (
            <tr key={index}>
               {val.inputArr.map((val2)=>{
                    return (
                      <th key={val2}>{val2}</th>
                    )
                })}
            </tr>
         )
    })
    return <div>{test}</div>
  }
}

ReactDOM.render(<App/>, document.getElementById('app'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="app"/>

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

2 Comments

this does not compile.
"," expected and the field goes red, you know compilation error
1

I see you already accepted an answer but I would like to elaborate on what was wrong with your code.

render() {
  const test=this.state.Data.map((val, index1)=>(
    <tr key={index1}>
      {val.inputArr.map( (val2, index2) => <td key={index2}>{val2}</td> )}
    </tr>
  ))

  return <table>{test}</table>;
}

Fiddle demo

  • JavaScript is case sensitive, your property is Data not data.
  • Take care using the correct {} or () with arrow functions. Using {} means you can make multiple statements and explicitly return something. Using () will automatically return the expression.
  • To output a value in React wrap it in {} like I did with {val2}
  • Given your data structure, you must use the inputarr propety when accessing the inner array.
  • Use a key on element lists. You can read about keys in react here.

1 Comment

thank you for your contribution, you cleared some things for me

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.