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

155
Views
Is there a way to store object accessors as variables in Javascript?

I have a script that I'd like to re-use for different projects where I'm calling similar APIs but the JSON object that I receive might be a little different. So, when I parse the data, I'd like to have control over what values I access.

For this particular API, the following works:

var contractData = theParsedJSONdata.data.items;

Going forward the keys data and items may vary. Can I use variables to access the object?

I tried this, but it didn't work:

let objectString = ".data.items";
var contractData = theParsedJSONdata + objectString;

Example data:

    { data: 
   { address: '0x000000000222221111111111',
     updated_at: '2022-02-03T00:30:12.793596926Z',
     next_update_at: '2022-02-03T00:35:12.793597076Z',
     quote_currency: 'USD',
     chain_id: 137,
     items: 
      [ [Object],
        [Object],
        [Object],
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can use variables as object keys with this notation:

key1 = "data";
key2 = "items";

obj[key1][key2] // equivalent to obj.data.items
about 4 years ago · Juan Pablo Isaza Report

0

Use a function.

let objectAccessor = obj => obj.data.items;
let contractData = objectAccessor(theParsedJSONdata);

You can later reassign objectAccessor:

objectAccessor = obj => obj.something.thing;
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!