I have the following object:
translations = {
'nl': {
'Dashboard': [
{
"Today's turnover": "Omzet van vandaag",
"Get an overview directly from your receipts on location.": "Bekijk een overzicht rechtstreeks vanuit uw inkomsten op locatie.",
"Choose your POS provider": "Kies uw POS provider"
}],
'Products': [],
'Order': []
}
}
And I'm trying to access the Today's turnover
property of the Dashboard
proprty inside nl
, which, according to the question and answers here can be accessed like object['object property']
However when I try to access it, it comes as undefined for some reason:
Why is this not working?
Dashboard is an array, and your object is the first element in this array. Also you don't have to escape '
if you are in a ""
string!
So use this:
translations['nl']['Dashboard'][0]["Today's turnover"]
Check below code its working for object['object property']
, may be you are using some wrong key name
translations = { 'nl': { 'Dashboard': [{ "Today's turnover": "Omzet van vandaag", "Get an overview directly from your receipts on location.": "Bekijk een overzicht rechtstreeks vanuit uw inkomsten op locatie.", "Choose your POS provider": "Kies uw POS provider" }], 'Products': [], 'Order': [] } } document.getElementsByTagName("div")[0].innerHTML = translations['nl']['Dashboard'][0]['Today\\'s turnover'];
<div></div>
Why is this not working?
Because you are not using the exact name of the property, but for some reason decided to add an extra backslash …
Dashboard
is a Array
, not Object
. To access it, you must:
translations['n1']['Dashboard'][0]['Today\'s turnover']
Or:
translations.n1.Dashboard[0]['Today\'s turnover']
Dashboard
is an array. So use Dashboard[0]
translations = {
'nl': {
'Dashboard': [
{
"Today's turnover": "Omzet van vandaag",
"Get an overview directly from your receipts on location.": "Bekijk een overzicht rechtstreeks vanuit uw inkomsten op locatie.",
"Choose your POS provider": "Kies uw POS provider"
}],
'Products': [],
'Order': []
}
}
console.log(translations.nl.Dashboard[0]['Today\'s turnover']);
Note
[ ]
is use to retrieve Today's turnover
key instead of dot(.)
notation. You can check this Link for more information
A bit of theory to back other good answers here.
Properties of JavaScript objects can also be accessed or set using a bracket notation (for more details see property accessors ). Objects are sometimes called associative arrays, since each property is associated with a string value that can be used to access it. So, for example, you could access the properties of the myCar object as follows:
myCar['make'] = 'Ford';
myCar['model'] = 'Mustang';
myCar['year'] = 1969;
For more, read on at Working with JS Objects .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.