Search code examples
angulargoogle-cloud-firestoreangularfire2angular8

how to parse Json object like an array in typescript . MAP inside MAP firestore


I want to parse driverDocumentDetails like an Array.

JSON is implemented from android (Java) HashMap data structure Angular Firestore MAP inside MAP

{ 
   "driverDocumentDetails":{ 
      "fhgfh":{ 
         "documentCategoryID":"fhgfh",
         "documentCategoryName":"Pan Card",
         "documentUploadedDateTime":null,
         "reasonForRejection":null,
         "uploaded":false,
         "uploadedDocumentURL":null,
         "verified":false
      },
      "bjhbh":{ 
         "documentCategoryID":"bjhbh",
         "documentCategoryName":"Driving License",
         "documentUploadedDateTime":null,
         "reasonForRejection":null,
         "uploaded":false,
         "uploadedDocumentURL":null,
         "verified":false
      },
      "hgvjh":{ 
         "documentCategoryID":"hgvjh",
         "documentCategoryName":"NOC",
         "documentUploadedDateTime":null,
         "reasonForRejection":null,
         "uploaded":false,
         "uploadedDocumentURL":null,
         "verified":false
      }
   },
   "driverID":"bhbjhbjhbj",
   "hgchg":[ 
      { 
         "bjnk":"jhbjhb",
         "fhfyhgf":"jjb"
      },
      { 
         "gfhg":"jgjuy",
         "gh":"guguj"
      }
   ]
}
onCustom(recordRow) {
    console.log("onCustom Driver docs data---->",recordRow);
    this.crudService.ReadDriverDocuments(recordRow.data.id).subscribe(data => {

    this.driverDocument = data.data();
    console.log('ReadDriverDocuments',this.driverDocument);
    });
  }

Solution

  • You can use Object.keys to get the keys and then map it into a new array:

    var driverDocumentDetailsArray = Object.keys(this.driverDocument.driverDocumentDetails)
        .map(key => this.driverDocument.driverDocumentDetails[key]);
    

    Or if it's available, just Object.values

    var driverDocumentDetailsArray = Object.values(this.driverDocument.driverDocumentDetails);