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

228
Views
Using String as JSON key for Firebase update()

The following JavaScript updates my firebase:

db.collection("users").doc(user.uid).update({
                         img1: pic
                      });

I have img1, img2, img3 in my database. I want to use a for loop to update the values like so:

for (let i = 1; i <= 3; i++) {
  let s = "img" + i;

  db.collection("users").doc(user.uid).update({
                         s: pic
                      });
}

Unfortunately this does not work. Is there another way to achieve this?

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

0

In your loop, change the code like this:

db.collection("users").doc(user.uid).update({
    [s]: pic
});

This way, you'll use the value of s as the name of the field to update.

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!