Nested Objects
Values in an object can be another object:
const myObj = {
name: "John",
age: 30,
cars: {
car1: "Ford",
car2: "BMW",
car3: "Fiat"
}
}
console.log(myObj); /
//output
{ name: 'John',
age: 30,
cars: { car1: 'Ford', car2: 'BMW', car3: 'Fiat' } }
You can access nested objects using the dot notation or the bracket notation:
console.log(myObj.cars.car1); //Ford
console.log(myObj.cars.car2); // BMW
console.log(myObj.cars.car3); //Fiat
myObj["cars"]["car2"];
myObj.cars["car2"];
Comments
Post a Comment