Can I use a map() inside a pushed object?

Viewed 32

I am using forEach to loop through a an array of objects. In the last one ("trans") I want to create a nested array of objects, just like my database:

enter image description here

But I have got an error in my code:

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
    })
})

What am I doing wrong?

THANKS

1 Answers

Firebase's DataSnapshot type doesn't implement a map function, so your childSnapshot.child('trans').map(...) won't work.

The equivalent working code would be:

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
    })
})
Related