简体   繁体   English

如何使用 Reactjs 将对象数据渲染到表

[英]How to render object data to table with Reactjs

I got a data from API and it seems like Object data(JSON) Public API Link我从 API 获得了一个数据,它看起来像对象数据(JSON)公共 API 链接

import React, { Component } from 'react';
import axios from 'axios';
import './PostContainer.css';

class PostContainer extends Component {
  constructor(props) {
    super(props);
    this.state = { chartData: {} };
  }

  async componentDidMount() {
    let { data: chartData } = await axios.get(
      'https://api.bithumb.com/public/ticker/all'
    );
    this.setState({ chartData });
  }

  render() {
    const { chartData } = this.state;
    // chartData.data.date = undefined;
    // delete chartData.data.date;
    console.log(`DATA ${chartData.data}`);

    // for (const val, index in chartData.data) {
    //   console.log(`${index} : ${val.sell_price}`)
    // }

    return (
      <div className="Post">
        <table id="table" className="table table-striped table-bordered">
          <thead>
            <tr>
              <th>Coin Name</th>
              <th>Current Price</th>
              <th>Volume</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>{?? What should I enter?}</td>
              <td>123123123123</td>
              <td>12312312</td>
            </tr>
            <tr>
              <td>ETH</td>
              <td>121243123</td>
              <td>1231231</td>
            </tr>
          </tbody>
        </table>
      </div>
    );
  }
}

export default PostContainer;

And I want to render all data to table like this.我想像这样将所有数据呈现到表格中。

在此处输入图片说明

With console.log, it returned Object like this使用console.log,它返回这样的对象

在此处输入图片说明

But I don't have any idea how to iterate to render the data with these object data.但我不知道如何迭代以使用这些对象数据呈现数据。 Before when I use Express & NodeJS & Pug environment I use for...in statement and it works well.在我使用 Express & NodeJS & Pug 环境之前,我使用 for...in 语句并且它运行良好。 But in Reactjs with this code但是在带有这段代码的 Reactjs 中

for (const val, index in chartData.data) {
      console.log(`${index} : ${val.sell_price}`)
    }

It got error like this它有这样的错误

  Line 24:  Parsing error: Unexpected token

I tried lots of another iteration method but it seems not right.我尝试了很多另一种迭代方法,但似乎不对。

If you meant to iterate the values Object.entries() can also do the trick just skip the key part.如果您打算迭代值Object.entries()也可以跳过关键部分。

for (const [key, value] of Object.entries(chartData.data)) {
  console.log(`${value.sell_price}`)
}

// Or, via forEach

Object.entries(chartData.data).forEach(([key, value]) => {
  console.log(`${value.sell_price}`) 
});

The listed API data is an object .列出的 API data是一个object To iterate over the object and use the data as well as the key , first lets get the keys from the object(inside your render ).要迭代对象并使用数据和key ,首先让我们从对象中获取keys (在您的render )。 This can be done using这可以使用

const dataKeys = Object.keys(chartData);

Next, lets iterate over the keys and generate a tr and the td and display the required data.接下来,让我们遍历键并生成trtd并显示所需的数据。 That would be similar to (inside your tbody )这将类似于(在您的tbody

{
  map(dataKeys, (k) => {
   const data = chartData[k];
    return (
       <tr>
          <td>{k}</td>
          <td>{data.sell_price}</td>
          <td>{data.units_traded}</td>
        </tr>
    );
  });
}

Place the above code at the appropriate place.把上面的代码放在合适的地方。 The logic used is, first get the keys of the object in an array.使用的逻辑是,首先获取数组中对象的keys Then map over them and return the required jsx that would be added to the table body.然后map它们并返回将添加到表体的所需jsx Hope this helps.希望这可以帮助。

Assumption: data (Object) in API points to chartData variable in the above code.假设:API中的data (Object)指向上述代码中的chartData变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM