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

136
Views
JavaScript Objects Target the value by the name of the key
const data = { bmw: { price: 200 } }

let carName = Object.keys(data).toString() 

console.log(carName)
// Output: bmw

let price = data.carName.price

console.log(price)
// Output: TypeError: Cannot read property 'price' of undefined

let price2 = data.bmw.price

console.log(price2)
// Output: 200

why I Can not use the name of the variable to access the price why i do have to type it by myself

thanks

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

0

You have to use:

let price = data[carName].price

console.log(price)

This is because the carName property does not exist on data object, but the key with the value of the carName variable exists on the data object, so you need to use square brackets to get the price of the car

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!