1
const myStorage = {
            'car': {
                'inside': {
                    'glove box': 'maps',
                    'passenger seat': 'crumbs'
                },
                'outside': {
                    'trunk': 'jack'
                }
            }
        };

I can understand 'glove box' is the property and its value is 'maps', but what about 'car', 'inside' and 'outside'. Are they also properties or the values of car? I can recognise with a comma that we have two objects here, but where do these start. Please elaborate on this.

Codewell
  • 13
  • 6
  • These are nested objects: for example, "inside" is a property of "car" and its value is another object, where its properties are "glove box": "maps" and "passenger seat": "crumbs" – LeoDog896 Oct 20 '22 at 12:08
  • Please see [How can I access and process nested objects, arrays, or JSON?](/a/11922384/4642212) and read the documentation about the [Object initializer](//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Object_initializer). – Sebastian Simon Oct 20 '22 at 12:12

3 Answers3

1

myStorage is an object with the property car. car is also an object with two properties, inside and outside

Properties are the values associated with a JavaScript object.

A JavaScript object is a collection of unordered properties.

So in this case, car is both an object (data type) and a property (value associated with myStorage)

I'd recommend reading up on JavaScript data structures: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures

Fralle
  • 889
  • 6
  • 12
  • With your explanation, can I assume "an object property can itself be an object"? Then, by that logic, inside and outside are also objects. In fact, all these (car, inside and outside) are objects and properties at the same time. I am unable to digest how they can be both at the same time. Does it mean we have three objects here? – Codewell Oct 21 '22 at 10:12
  • That is correct, an object can contain other objects. Property and data type are two different things: an apple can be a fruit (data type) but can also hang from a tree (property) – Fralle Oct 21 '22 at 10:48
0

Yes, they are also properties. You need to know that these values are nested objects. E.g. If you want to access "passenger seat".

myStorage.car.inside["passenger seat"]
0

I think this example will clarify things for you and help you understand that.

const myStorage = {
    'car': {
        'seats': {
            'number': '4 seats',
            'color': 'white'
        },
        'wheels': {
            'numbers': '4 wheels',
            'color': 'black'
        }
    }
};

console.log(myStorage.car.seats);
console.log(myStorage.car.seats.color);
Mad7Dragon
  • 1,237
  • 1
  • 10
  • 21