This is not an JavaScript object:
Customer[id: "0001"; name: "ivan" ; country [city : "Peru"]]
This is an object in JavaScript object:
var customer = {
id: "0001",
name: "ivan",
country: {
finland: {
city: "Helsinki"
}
}
};
You would use it with this:
console.log(customer.country.finland.city);
or this:
console.log(customer['country']['finland']['city']);
or with mixing....
console.log(customer['country'].finland.city);
..and yes took a freedom of adding actual country, not just city but I guess this post demonstrates the point, you can either retrieve values with dot notation customer.country.finland or apostrophes customer['country']['finland'] .. However, if you are using numbers as JavaScript object key like this:
var customer = {
1: "Mauno"
};
You can retrieve it only using apostrophes as: customer['1'] trying to use it like customer.1 will result in a JavaScript error.
Object == {} && Array == []. Use a proper syntax.[]are used to retrieve a value from an array or object at a particular index. The index is the value within the brackets.