This indicates that the first element of your array (array[0]) is indeed an object, and this object has a property named name with the value "anna". The structure of this object can be represented as:
{
"name": "anna"
}
2. array[0].anna says "can't get property of undefined"
Here's the key point: when you use dot notation (.) to access a property in JavaScript, the part after the dot is interpreted as a literal property name. So, array[0].anna is attempting to access a property named "anna" on the object array[0].
However, based on what you've shown, the object array[0] has a property named name with the value "anna", not a property named "anna". This is why you're getting the error: JavaScript is looking for a property named anna on the object, which doesn't exist, hence "can't get property of undefined".
To access the value "anna", you should use the actual property name, which is name:
console.log(array[0].name); // Outputs: "anna"
Alternatively, you can use bracket notation, which is useful for dynamic property access or when property names are determined at runtime:
console.log(array[0]['name']); // Outputs: "anna"
Additional Example for Clarity:
Suppose your array's first element was an object with multiple properties, including one named anna:
In this hypothetical case, both of the following would work:
console.log(array[0].name); // Outputs: "john"
console.log(array[0].anna); // Outputs: "some value here"
But remember, the property names are case-sensitive, so array[0].Anna would again result in "can't get property of undefined" in both scenarios discussed.
Edge Case: Nested Objects
If you're dealing with nested objects, you can chain dot notation or bracket notation to access deeper properties. For example: