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

152
Views
Cómo acceder a la propiedad del nombre del objeto

Considere una const que contiene un objeto con múltiples niveles como este:

 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'], }, };

Sé que, por ejemplo, si quiero acceder a valor del primer objeto, puedo decir test.One.a , pero ¿cómo accedo mediante programación al valor de test.One , test.Two , etc. sin codificarlo?

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

0

Use la notación de corchetes para acceder a las propiedades:

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

Estos funcionan con cadenas, por lo que puede usar 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!