Try to search your question here, if you can't find : Ask Any Question Now ?

Not understanding this map() behavior

HomeCategory: stackoverflowNot understanding this map() behavior
kundan asked 2 weeks ago

I have a Cloud Firestore document that is supposed to be an array of objects. It looks like this

docname: {
  0: {...}
  1: {...}
  2: {...}

This is expected, because I populated that document like this

myDocRef.set( {...user.myArray});

The problem I have is when I go to retrieve the data from the document.
I currently do it like this

getUserItems(uid) {
  return this._db.doc<MyObj>(`users/${uid}/mysubcollection/items`).valueChanges(); 
}

That returns an object of objects and I need it to be an array of objects so that I can use forEach() on it.

I tried using the map() operator but its giving me some results I am not understanding.

So, my goal is to convert that object of objects to an array of objects and I tried this

items$.pipe(
      tap(item => console.log('before map', item )),
      map(item => [item]),
      tap(item => console.log('after map', item ))
    )
    .subscribe(...);

This does kind of work since it converts the object of objects to an array of objects but of course it puts all objects into the first dim of a two dim array to it looks like this

Array [Object(9)]

What can I do here to convert an object of objects to an array of objects so that my end result is like this

Array(9) [ {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…} ]

So going from this

Object(9) [ {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…} ]

to this

Array(9) [ {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…} ]
1 Answers
Best Answer
Arben answered 2 weeks ago
Your Answer

18 + 7 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com