【问题标题】:Can I use a map() inside a pushed object?我可以在推送对象中使用 map() 吗?
【发布时间】:2021-07-13 06:41:38
【问题描述】:

我正在使用forEach 循环一个对象数组。在最后一个(“trans”)中,我想创建一个嵌套的对象数组,就像我的数据库一样:

但我的代码中有错误:

snap.forEach(childSnapshot => {
    allWorks.push({
        id: childSnapshot.key,
        link: childSnapshot.val().link,
        trans: childSnapshot.child('trans').map(childSnapshot => { lang: childSnapshot.key}) // <- error: childSnapshot.child(...).map is not a function
    })
})

我做错了什么?

谢谢

【问题讨论】:

    标签: json firebase firebase-realtime-database


    【解决方案1】:

    Firebase 的 DataSnapshot type 没有实现 map 函数,因此您的 childSnapshot.child('trans').map(...) 将不起作用。

    等效的工作代码是:

    snap.forEach(childSnapshot => {
        cont trans = [];
        childSnapshot.child('trans').forEach((transChild) => {
          trans.push(transChild.key);
        })
        allWorks.push({
            id: childSnapshot.key,
            link: childSnapshot.val().link,
            trans: trans
        })
    })
    

    【讨论】:

      猜你喜欢
      • 2021-04-14
      • 2012-04-29
      • 1970-01-01
      • 2022-12-22
      • 1970-01-01
      • 2017-07-29
      • 2023-03-31
      • 1970-01-01
      相关资源
      最近更新 更多