JavaScript:如何将具有嵌套数组的对象映射到React的表
我正在从Firebase中提取数据,并尝试使用React将其显示在表中 以下是将数据阵列记录到控制台时数据的外观:JavaScript:如何将具有嵌套数组的对象映射到React的表,javascript,arrays,reactjs,Javascript,Arrays,Reactjs,我正在从Firebase中提取数据,并尝试使用React将其显示在表中 以下是将数据阵列记录到控制台时数据的外观: Sampling0: {Time: "1575031318", Values: Array(6)} Sampling1: {Time: "1575031965", Values: Array(6)} Sampling2: {Time: "1575032607", Values: Array(6)} Sampling3: {Time: "1575033253", Values: Ar
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)}
以下是采样1的扩展数据:
Sampling1:
Time: "1575031965"
Values: Array(6)
Spot0: 31
Spot1: 32
Spot2: 7
Spot3: 32
Spot4: 11
Spot5: 18
我试图将此数据映射到一个包含三列的表中,例如:
点#|值|采样周期
点0 | 31 |采样周期1
Spot 1 | 32 |采样周期1如果我理解正确,那么一种方法是预处理firebase返回的数据,如下所示:
const数据={
抽样1:{时间:“1575031965”,数值:[
{Spot0:31},
{Spot1:32},
{Spot2:7},
{Spot3:32},
{Spot4:11},
{Spot5:18}
]},
采样2:{时间:“1575032607”,值:[
{Spot0:42},
{Spot1:41},
]},
抽样3:{时间:“1575033253”,值:[
{Spot0:1},
{Spot1:2},
{Spot2:3}
]}
};
const tableData=Object.entries(data).reduce((arr,samplingEntry)=>{
//提取当前采样项的键和值
const[samplingKey,samplingObject]=抽样企业;
const smaplingPeroidLabel=samplingKey.replace('Sampling','Sampling period');
//迭代采样对象的值数组的前两个点项
for(设i=0;i
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>