I am pulling data from Firebase and I am trying to display it in a table with 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:
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 | 31 | Sampling Period 1
Spot 1 | 32 | Sampling period 1 <-- Then move to sampling period 2
Spot 0 | 42 | Sampling Period 2
Spot 1 | 41 | Sampling period 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>