简体   繁体   English

JavaScript:如何将带有嵌套数组的对象映射到 React 表

[英]JavaScript: How to map object with nested arrays to table for React

I am pulling data from Firebase and I am trying to display it in a table with React.我正在从 Firebase 中提取数据,并尝试使用 React 将其显示在表格中。

Here is what my data looks like when I log the data array to the console:当我将数据数组记录到控制台时,我的数据如下所示:

Sampling0: {Time: "1575031318", Values: Array(6)}
Sampling1: {Time: "1575031965", Values: Array(6)}
Sampling2: {Time: "1575032607", Values: Array(6)}
Sampling3: {Time: "1575033253", Values: Array(6)}
Sampling4: {Time: "1575033926", Values: Array(6)}
Sampling5: {Time: "1575034577", Values: Array(6)}

Here is what the expanded data looks like for sampling1:以下是 Sampling1 的扩展数据:

Sampling1:
Time: "1575031965"
Values: Array(6)
Spot0: 31
Spot1: 32
Spot2: 7
Spot3: 32
Spot4: 11
Spot5: 18

I am trying to map this data into a table with three columns for example:我正在尝试将此数据映射到包含三列的表中,例如:

Spot # |现货# | Value |价值 | Sampling Period采样周期

Spot 0 |现货 0 | 31 | 31 | Sampling Period 1采样周期 1

Spot 1 |现货 1 | 32 | 32 | Sampling period 1 <-- Then move to sampling period 2采样周期 1 <-- 然后移动到采样周期 2

Spot 0 |现货 0 | 42 | 42 | Sampling Period 2采样期 2

Spot 1 |现货 1 | 41 | 41 | Sampling period 2采样期2

Here's my code so far:到目前为止,这是我的代码:

componentDidMount() {
this.renderCharts();
};

renderCharts() {
  const wordRef = firebase.database().ref('myTable');
  wordRef.on('value', (snapshot) => {
    let words = snapshot.val().myData;
    let newState = [];
    let myState = [];
    for (let word in words) {
      newState.push({
        Time: word,
        Values: words[word],
      })
}

 render() {
    return (
      <div className="animated fadeIn">
                 <Table responsive>
                        <thead>
                        <tr>
                          <th>Spot #</th>
                          <th>Values</th>
                          <th>Sampling period</th>
                        </tr>
                        </thead>
                        <tbody>
                          {this.state.words.map((d, index) => {
                              return [
                                  <tr key={d.index}>
                                    <td>?</td>
                                    <td>{d.Values[index]}</td> //This grabs the value in the proper index but for different sampling periods
                                    <td>{index}</td> //This properly displays sampling period #'s
                                  </tr>
                              ];
                            })}

                        </tbody>
                      </Table>
      </div>

If I understand correctly then one approach would be to preprocess the data returned from firebase using reduce as shown below:如果我理解正确,那么一种方法是使用 reduce预处理从 firebase 返回的数据,如下所示:

 const data = { Sampling1: {Time: "1575031965", Values: [ { Spot0: 31 }, { Spot1: 32 }, { Spot2: 7 }, { Spot3: 32 }, { Spot4: 11 }, { Spot5: 18 } ]}, Sampling2: {Time: "1575032607", Values: [ { Spot0: 42 }, { Spot1: 41 }, ]}, Sampling3: {Time: "1575033253", Values: [ { Spot0: 1 }, { Spot1: 2 }, { Spot2: 3 } ]} }; const tableData = Object.entries(data).reduce((arr, samplingEntry) => { // Extract key and value of current sampling entry const [samplingKey, samplingObject] = samplingEntry; const smaplingPeroidLabel = samplingKey.replace('Sampling', 'Sampling period '); // Iterate first two spot items of Values array for sampling object for(let i = 0; i < Math.min(2, samplingObject.Values.length); i++) { // Extract the first entry of this spot object const [spotEntry] = Object.entries(samplingObject.Values[i]); // Extract key and value of this spot entry const [spotKey, spotValue] = spotEntry; // Add spot key, spot value and sampling key as new row of arr arr.push([ spotKey, spotValue, smaplingPeroidLabel ]); } return arr; }, []); console.log(tableData);

The script above basically transforms your server response to an "array of arrays", where each sub array corresponds to a row of data in the table that you are rendering.上面的脚本基本上将您的服务器响应转换为“数组数组”,其中每个子数组对应于您正在呈现的表中的一行数据。

During your components render() phase, this data would first be mapped to <tr> elements and, where a secondary mapping would render the contents of that row element into <td> elements, eg:在您的组件render()阶段,此数据将首先映射到<tr>元素,并且二级映射将该行元素的内容呈现为<td>元素,例如:

// Existing JSX
<tbody>
{ tableData.map(row => <tr>{ row.map(cell => <td>{ cell }</td> }</tr>) }
</tbody>
// Existing JSX

Hope that helps!希望有帮助!

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

相关问题 如何在本机反应中嵌套 arrays 的 map object - How to map object of nested arrays in react native 将JavaScript对象/地图转换为嵌套数组? - Converting JavaScript object/map into nested arrays? 试图从嵌套子数组的 object 到 HTML 表中的单元格中的 map 元素,不确定如何到达最深的子 ZA11554F9DB0CED7F2E - Trying to map elements from object of nested subarrays to a cell in an HTML table, unsure how to reach deepest sub arrays 在javascript中映射嵌套数组中的非对象值 - map non-object values in nested arrays in javascript Javascript将2个数组映射为1个对象 - Javascript map 2 arrays into 1 Object 比较嵌套的 object arrays 和存在的返回值 JavaScript, React - Comparing nested object arrays and returning values that exist JavaScript, React React - 如何映射嵌套对象值? - React - how to map nested object values? React Map函数:如何一次输出所有嵌套数组的内容? - React map function: how to output content of all nested arrays at once? 如何在反应js中来自两个嵌套的arrays的map数据? - How to map data from two nested arrays in react js? 如何 map 按所需 object 值分组的嵌套 arrays 数组 - How to map an array of nested arrays grouped by desired object values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM