React - 在表格中渲染数据

时间:2017-12-28 23:52:17

标签: javascript reactjs redux

我有一个状态对象,如下所示:

{
"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
}

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

sample image

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

有人有解决方法吗?

3 个答案:

答案 0 :(得分:0)

这是一个让你入门的例子。希望这会有所帮助。

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>
    );
  }
}

答案 1 :(得分:0)

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

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>
}

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

答案 2 :(得分:0)

&#13;
&#13;
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)
&#13;
&#13;
&#13;

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