Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

138
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda