Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

250
Views
Get map key name and values stored on array from firestore

I'm trying to get the values from a map and store it in an array from firestore using react native. This is an example of how I would like the array to look, after the map gets pulled.

const [food, setFood] = useState([
    {Food: 'Cheese', Calories: 20, Quantity: 1, key: '1'},
    {Food: 'Lettuce', Calories: 10, Quantity: 1, key: '2'},

]);

Here is the stored data in firestore: firestore data

For getting the data and other fields from firestore I'm using this function:

const getFoodData = async () =>{

    const docRef = doc(db, "foods", uid);
    const docSnap = await getDoc(docRef);
    
    if (docSnap.exists()) {
      console.log("Document data:", docSnap.data());
    } else {
      console.log("No such document!");
    }
        setFood(docSnap.get("Food.Cheese.Calories"))
        console.log(food)
}

I'm able to get the value from an individual field by doing the above, but I need to have it stored in an array, like the first example. how would I be able to do that?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can try using Object.keys() and then use a map as shown below:

const foodMap = docSnap.data().Food

const result = Object.keys(foodMap).map(key => ({ food: key, ...foodMap[key] }))
about 4 years ago · Juan Pablo Isaza Report

0

Great answer, Dharmaraj.

Adding code snippet so you can see it in action -

// Food is the result of doc.data().food
const food = {
  Cheese: { Calories: 20, Quantity: 1, key: '1'},
  Lettuce: { Calories: 10, Quantity: 1, key: '2'},
};

console.log('Firestore Data: ', food)

const result = Object.keys(food).map(key => ({ food: key, ...food[key]}))

console.log('Flattened result: ', result) 

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!