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

127
Vistas
Why do I have an undefined when I try to read object[key].value?

I want something similar to this :

Cars {
    "test-01-test" : {
        brand: 'anybrand',
        color: 'blue,
    }

I want Cars as an Object and the id "test-01-test" as an Object too.

So I can read it like that :

console.log(this.cars["test-01-test"].color) //result should be blue

I tried this in my data function

cars : {
        id : {
            brand : '',
            color : '',
        }
    }

and then in my mounted in I wrote this :

    this.cars.id = "test-01-test";
    this.cars.id["test-01-test"].color="blue";
    console.log(this.cars);

But the console says that :

Cannot set property 'color' of undefined

I want to set-up the color and the like my first exemple, how can I do ?

What I'm trying to do, is the add IDs dynamically. If I do something like this (imagine I ask the users with a modal to insert a new car with a new id):

this.cars[id] = "test-02-test" //test-02-test was given by the user's input

I want to create a new object with the value "test-02-test" and then put the color and the brand so my data looks like this :

Cars {
    "test-01-test" : {
        brand: 'anybrand',
        color: 'blue,
    },
    "test-02-test" : {
        brand: 'anybrand',
        color: 'blue,
    }
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

What you did is:

let cars = {
  id: {
    brand: '',
    color: '',
  }
};

cars.id = "test-01-test";

console.log(cars);

Instead you should do:

let cars = {
  id: {
    brand: '',
    color: '',
  }
};

console.log(cars);
cars.id.color = "blue";
console.log(cars);

or dynamically:

let cars = {
  peugeot: {
    brand: '',
    color: '',
  },
  citroen: {
    brand: '',
    color: '',
  }
};

let id = 'citroen'
cars[id].color = "blue";
console.log(cars);

You can add a new id like below:

let cars = {
  peugeot: {
    brand: '',
    color: '',
  },
  citroen: {
    brand: '',
    color: '',
  }
};

let id = 'ford'
cars[id] = {
  brand: '',
  color: '',
};
console.log(cars);

or with https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

let cars = {
  peugeot: {
    brand: '',
    color: '',
  },
  citroen: {
    brand: '',
    color: '',
  }
};

cars = {...cars, dodge: {
    brand: '',
    color: '',
    }
}
console.log(cars);

about 4 years ago · Juan Pablo Isaza Denunciar

0

Your ID is a key inside this object

Try:

let cars = {
  "test-01-test" : {
    brand : 'anybrand',
    color : 'blue',
  }
}
console.log(cars["test-01-test"].color)
// Output: blue
about 4 years ago · Juan Pablo Isaza Denunciar

0

Because identifier is case sensitive. A function define in global scope, 'this' refer in this function window object, when you access 'this' in function it refer window.cars. So, window.cars is not equal to window.Cars.

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