【问题标题】:React - render data in a tableReact - 在表格中呈现数据
【发布时间】:2018-06-09 11:50:49
【问题描述】:

我有一个处于以下状态的对象:

{
"data": {
    "available": {
        "profileOne": {
            "a": 14,
            "b": 14,
            "c": 0,
            "d": 0,
            "e": 18
        },
        "profileTwo": {
            "a": 13,
            "b": 9,
            "c": 0,
            "d": 0,
            "e": 18
        },
        "profileThree": {
            "a": 12,
            "b": 12,
            "c": 0,
            "d": 0,
            "e": 14
        },
        "profileFour": {
            "a": 3,
            "b": 3,
            "c": 0,
            "d": 0,
            "e": 5
        },
        "profileFive": {
            "a": 6,
            "b": 3,
            "c": 0,
            "d": 0,
            "e": 11
        }
    },
    "isFetching": false
}

我需要按照下面提供的示例图片使用数据:

我知道为了能够映射这些,我需要将其转换为数组,这几乎就是我所处的位置。

有没有人可以解决这个问题?

【问题讨论】:

标签: javascript reactjs redux


【解决方案1】:

这是一个帮助您入门的示例。希望这会有所帮助。

const json = {
  data: {
    available: {
      profileOne: {
        a: 14,
        b: 14,
        c: 0,
        d: 0,
        e: 18
      },
      profileTwo: {
        a: 13,
        b: 9,
        c: 0,
        d: 0,
        e: 18
      }
    }
  }
};

const header = ["pro", "a", "b", "c", "d", "e"];
class App extends React.Component {
  render() {
    return (
      <table>
        <thead>
          <tr>{header.map((h, i) => <th key={i}>{h}</th>)}</tr>
        </thead>
        <tbody>
          {Object.keys(json.data.available).map((k, i) => {
            let data = json.data.available[k];
            return (
              <tr key={i}>
                <td>{k}</td>
                <td>{data.a}</td>
                <td>{data.b}</td>
                <td>{data.c}</td>
                <td>{data.d}</td>
                <td>{data.e}</td>
              </tr>
            );
          })}
        </tbody>
      </table>
    );
  }
}

【讨论】:

    【解决方案2】:

    您需要遍历数据,但由于它是一个对象,您需要将其修改为可映射。这是一个可能的渲染函数:

    render(){
        const tableData = data.available
        return 
             (<table>
                  <tr>
                      <td>Profile</td>
                      <td>a</td>
                      <td>b</td>
                      <td>c</td>
                      <td>d</td>
                      <td>e</td>
                  </tr>
                  {(Object.keys(tableData))
                      .map(key=>({...tableData[key],title:key}))
                      .map(row=>(
                          <tr>
    
                              {(Object.keys(row))
                                   .map(key=>
                                       <td>
                                           {row[key]}
                                       </td>
                                   )
                              )}
                          </tr>
                      )
              </table>
    }
    

    请注意,如果您可以修改数据源以提供数组,这会简单得多。对象需要重构才能被映射,所以它使代码更加复杂。

    【讨论】:

    • 我的答案和@Kunukn 的答案的不同之处在于,这个答案将通过自动添加列来随您的数据扩展。
    【解决方案3】:

    const obj = {
    "data": {
        "available": {
            "profileOne": {
                "a": 14,
                "b": 14,
                "c": 0,
                "d": 0,
                "e": 18
            },
            "profileTwo": {
                "a": 13,
                "b": 9,
                "c": 0,
                "d": 0,
                "e": 18
            },
            "profileThree": {
                "a": 12,
                "b": 12,
                "c": 0,
                "d": 0,
                "e": 14
            },
            "profileFour": {
                "a": 3,
                "b": 3,
                "c": 0,
                "d": 0,
                "e": 5
            },
            "profileFive": {
                "a": 6,
                "b": 3,
                "c": 0,
                "d": 0,
                "e": 11
            }
        },
        "isFetching": false
    }}
    
    const entries = Object.entries(obj.data.available).map(data => Object.entries(data))
    console.log(entries)

    使用Object.entries 将为您提供一组可以映射的数组和子数组。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-03
      • 2021-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-22
      • 1970-01-01
      相关资源
      最近更新 更多