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

251
Views
How to query a key via a variable?

I want to set the value of a JSON key by a variable to setCache with the star and then the function sets the key value in the JSON object cache.

I have code it how I think it makes sense, however I know this is wrong because star is not in the object.

I also thought of using template strings.

My code

var cache = {
    "capricorn": "this should be null",
    "aquarius": "null",
    "pisces": "null",
    "aries": "null",
    "taurus": "null",
    "gemini": "null",
    "cancer": "null",
    "leo": "null",
    "virgo": "null",
    "libra": "null",
    "scorpio": "null",
    "ophiuchus": "null",
    "sagittarius":"null"
}

function setCache(cache,star, value) {
    cache.star = value
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use the subscript ([]) operator to refer to a property using a variable:

cache[star] = value;
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!