[英]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.