[英]How do we read one level nested deep array data from firebase in react js?
In my firebase database my data looks like below在我的 firebase 数据库中,我的数据如下所示
my code to read data from firebase as below我从 firebase 读取数据的代码如下
export const startSetPartialExpenses = (() => {
return (dispatch, getState) => {
const uid = getState().auth.uid
return database.ref(`users/${uid}/partialpayments`).once('value').then((snapshot) => {
const partialExpenses = []
snapshot.forEach((childSnapshot) => {
partialExpenses.push({
id: childSnapshot.key,
...childSnapshot.val()
})
})
dispatch(setPartialExpense(partialExpenses))
})
}
})
after reading data from firebase, It has data like below when my component initial load从 firebase 读取数据后,当我的组件初始加载时,它具有如下数据
Here I am copying data to local component object from redux store which is coming from the firebase database.在这里,我将来自 firebase 数据库的 redux 存储中的数据复制到本地组件 object。
partialExpense: props.expense && props.expense.paidStatus === 'Partial Paid' ? props.partialExpense : [],
below I added map logic to read all the rows下面我添加了 map 逻辑来读取所有行
<tbody className="create__partial-tbody">
{this.state.partialExpense.map((row) => (
<tr className="create__partial-tr" key={row.balance}>
<td>
<select
value={row.partialPaidStatus}
onChange={(event) => this.handlepartialPaidStatusChange(row.balance, event)}
>
<option value="Select"> {` `} </option>
<option value="Partial Paid">Partial Paid</option>
<option value="Paid">Paid</option>
</select>
</td>
<td >
<input
type="text"
placeholder="Amount"
value={row.partialAmount}
onChange={(event) => this.handlepartialAmountChange(row.balance, event)}
/>
</td>
after adding above code, I ran my code locally machine and it is not displaying my data, it is displaying default values.添加上面的代码后,我在本地机器上运行了我的代码,它没有显示我的数据,它显示的是默认值。
To get the result you are expecting will require two iterations, one for each key and one for each 'row'.要获得您期望的结果,需要两次迭代,每个键一次,每个“行”一次。 Since map's only work on arrays, you need to convert it to an iterable where we can also store the key should you need to update it in the future.
由于 map 仅适用于 arrays,因此您需要将其转换为可迭代的,我们还可以在其中存储密钥,以防您将来需要更新它。 to preserve your structure as the context of your needs is unclear, I will map it to an array of objects.
为了保留您的结构,因为您的需要的上下文不清楚,我将 map 放到一个对象数组中。
const result = []
for (const [key, row] of Object.entries(data.val())) {
for (const [index, value] of Object.entries(row)) {
result.push({payKey: key, entry: index, ball: value.balance});
}
}
from there, you should be able to manipulate the loop and code in use per your apps design.从那里,您应该能够根据您的应用程序设计来操作循环和使用的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.