简体   繁体   English

我们如何在 React js 中从 firebase 中读取一级嵌套深度数组数据?

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

相关问题 我们如何提高使用递归组件呈现深层嵌套树数据的 React 应用程序的性能 - How do we improve the performance of a react application which renders the the deep nested tree data with a recursive component 如何从 firebase firestore(React js)读取数组文档字段 - How to read array document field from firebase firestore (React js) 如何从react js嵌套数组js文件中提取数据? - how to extract data from react js nested array js file? 从 javascript firebase 数据库读取数据时,如何读取嵌套的多个引用? - How can we read multiple references nested while reading data from javascript firebase database? 从JS数据结构中删除一级嵌套数组 - Remove one level of nested arrays from JS data structure 如何基于嵌套数组 4 级深度过滤数组? - How to filter an array based on a nested array 4 level deep? 如何在深度嵌套数组中循环递归? JS - How to loop with recursion in deep nested array ? JS 猫鼬:如何在嵌套数组上填充(深层) - Mongoose: how to populate on nested array ( deep-level) JavaScript:如何通过嵌套在 object 2 层深的值过滤数组 - JavaScript: How to filter array by values nested in object 2 level deep 使用包含检查深度数组中的数据(Firebase检索数据JS) - Checking data in deep array with includes (Firebase retrieve data JS)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM