0

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:

enter image description here

Why is this not working?

Community
  • 1
  • 1
iuliu.net
  • 6,666
  • 6
  • 46
  • 69

6 Answers6

4

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"]
Lux
  • 17,835
  • 5
  • 43
  • 73
2

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>
Ranjit Singh
  • 3,715
  • 1
  • 21
  • 35
1

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 …

CBroe
  • 91,630
  • 14
  • 92
  • 150
1

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']
Huy Chau
  • 2,178
  • 19
  • 26
0

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

jsfiddle

Community
  • 1
  • 1
brk
  • 48,835
  • 10
  • 56
  • 78
0

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.

Ahmad Awais
  • 33,440
  • 5
  • 74
  • 56