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
How to access object's name property

Consider a const that holds an object with multiple levels like this:

const test = {
  One: {
    a: 220,
    b: ['Chipotle', 'Subway', 'Habit Burger'],
  },
  Two: {
    a: 110,
    b: ['Chipotle'],
  },
  Three: {
    a: 200,
    b: ['Subway', 'Jack In The Box'],
  },
  Four: {
    a: 460,
    b: ['Chipotle', 'Subway', 'Habit Burger', 'Jack In The Box'],
  },
};

I know that, for example, if I want to access the first object's a value, I can say test.One.a, but how do I programmatically access the value of test.One, test.Two, etc. without hardcoding it?

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

0

Use the bracket notation for accessing properties:

test["One"]["a"];
test["Two"]["b"];

These work with strings, so you can use variables:

const test = {
  One: {
    a: 220,
    b: ['Chipotle', 'Subway', 'Habit Burger'],
  },
  Two: {
    a: 110,
    b: ['Chipotle'],
  },
  Three: {
    a: 200,
    b: ['Subway', 'Jack In The Box'],
  },
  Four: {
    a: 460,
    b: ['Chipotle', 'Subway', 'Habit Burger', 'Jack In The Box'],
  },
};

const first = Object.keys(test)[Math.floor(Math.random() * 4)];
const second = ["a", "b"][Math.floor(Math.random() * 2)];

console.log(first, second, test[first][second])

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!