0

I have an array of objects and I want to display it's values in a Table This is how my array looks like:

[{name: 'x', mobile: 'xxx'}, {name: 'y', mobile: 'yyy'}, ......]

I want to display it inside a table. This is what I tried so far

import React, { Component } from 'react';
import {
    Table,
    ProgressBar
} 
from 'react-bootstrap';

class Display extends Component {

  render() {
    var records = this.props.googleData;
    const API = this.props.api;
    const placeURI = this.props.placeURI;
    var rows = [];
    for(let p_id of records.results){
        let dataURI = `${placeURI}${p_id.place_id}${API}`;
        let proxyUrl = 'https://cors-anywhere.herokuapp.com/',
        targetUrl = dataURI
        fetch(proxyUrl + targetUrl)
        .then((res) => res.json())
        .then((data) => {
            let jsonData = JSON.parse(JSON.stringify(data));
            //console.log(jsonData);
            rows.push(jsonData.result);
        })
        .catch((e)=> console.log(`Error! ${e.message}`));
    }
    console.log(rows);

    return (
        <div>
            <ProgressBar now={45} />
            <Table striped bordered condensed hover responsive>
              <thead>
                <tr>
                  <th>#</th>
                  <th>Name</th>
                  <th>Full Address</th>
                  <th>Phone Number</th>
                  <th>International P.no</th>
                  <th>Website</th>
                  <th>Rating</th>
                </tr>
              </thead>
              <tbody>
                {rows.map(( listValue, index ) => {
                  return (
                    <tr key={index}>
                      <td>{listValue.name}</td>
                      <td>{listValue.title}</td>
                      <td>{listValue.price}</td>
                    </tr>
                  );
                })}
              </tbody>
            </Table>
        </div>
    );
  }
}

export default Display;

This is how my array looks enter image description here

But the map() is not returning any row. And if there is any suggestion by which I can improve my code is extremely appreciable. Please help

1 Answer 1

1
    import React, { Component } from 'react';
    import {
        Table,
        ProgressBar
    } 
    from 'react-bootstrap';

    class Display extends Component {
        constructor(props) {
            super(props);
            this.state={
                rows: []
            }
        }


    componentDidMount = () => {
        var records = this.props.googleData;
        const API = this.props.api;
        const placeURI = this.props.placeURI;
        var rows = [];
        for (let p_id of records.results) {
            let dataURI = `${placeURI}${p_id.place_id}${API}`;
            let proxyUrl = 'https://cors-anywhere.herokuapp.com/',
                targetUrl = dataURI
            fetch(proxyUrl + targetUrl)
                .then((res) => res.json())
                .then((data) => {
                    let jsonData = JSON.parse(JSON.stringify(data));
                    //console.log(jsonData);
                    rows.push(jsonData.result);
                })
                .catch((e) => console.log(`Error! ${e.message}`));
        }
        this.setState({
            rows:rows
        })
        console.log(rows);
    };

      render() {

    return (
        <div>
            <ProgressBar now={45} />
            <Table striped bordered condensed hover responsive>
              <thead>
                <tr>
                  <th>#</th>
                  <th>Name</th>
                  <th>Full Address</th>
                  <th>Phone Number</th>
                  <th>International P.no</th>
                  <th>Website</th>
                  <th>Rating</th>
                </tr>
              </thead>
              <tbody>
                {this.state.rows.map(( listValue, index ) => {
                  return (
                    <tr key={index}>
                      <td>{listValue.name}</td>
                      <td>{listValue.title}</td>
                      <td>{listValue.price}</td>
                    </tr>
                  );
                })}
              </tbody>
            </Table>
        </div>
    );
  }

    }

    export default Display;
Sign up to request clarification or add additional context in comments.

2 Comments

I'm Adding an Image of my Array
@Waeez Can you do console.log(this.state.rows) inside render method and let me know the result.

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.