Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

267
Views
how to print an array from an object in react?
const api = axios.create({
  baseURL: 'https://iotserver-arha83.herokuapp.com/api/customers/'
})
class App extends Component {
  state = {
    courses:[]
  }
  constructor(){
    super();
    api.get('/').then(res =>{
      this.setState({courses: res.data})
    })
  }
  render(){return (
    <div className="App">
      <header className="App-header">
        <table>
        <tr><td>pk</td>{this.state.courses.map(course=><td>{course.pk}</td>)}</tr>
        <tr><td>title</td>{this.state.courses.map(course=><td>{course.title}</td>)}</tr>
        <tr><td>gateway</td>{this.state.courses.map(course=><td>{course.gateway}</td>)}</tr>
        </table>     
      </header>
    </div>
  );}
}
export default App;
[
    {
        "pk": 1, 
        "title": "first", 
        "gateways": [1]
    }, {
        "pk": 2, 
        "title": "second", 
        "gateways": [2]
    }, {
        "pk": 5, 
        "title": "test", 
        "gateways": []
    }, {
        "pk": 6, 
        "title": "test", 
        "gateways": []
    }
]

this is my code and I want to print every item on my website but I can't print gateways because it's an array I tried to change the gateway into string so it might be printed but didn't work any ideas...? note: this code should print every thing on the upper list

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You need to iterate over the array only once using the map() method and render a tr for each element.

<table>
  <tr>
    <th>pk</th>
    <th>title</th>
    <th>gateway</th>
  </tr>
  {courses.map((course) => (
    <tr key={course.pk}>
      <td>{course.pk}</td>
      <td>{course.title}</td>
      <td>{course.gateways.join(",")}</td>
    </tr>
  ))}
</table>

Property gateways is also an array which you can map too, however since you only want to render values you can use join(",") method instead.

Edit zealous-panini-kfw4u

On a side note, it's better to start using functional components.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!