简体   繁体   English

无法访问名称中有空格的属性

[英]Cannot access property with space in name

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:我正在尝试访问nl Dashboard属性的Today's turnover属性,根据这里的问题和答案可以像object['object property']访问它,但是当我尝试访问它时,它是未定义的出于某种原因:

在此处输入图片说明

Why is this not working?为什么这不起作用?

Dashboard is an array, and your object is the first element in this array. Dashboard 是一个数组,您的对象是该数组中的第一个元素。 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检查下面的代码是否适用于object['object property'] ,可能是您使用了一些错误的键名

 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 . Dashboard是一个Array ,而不是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. Dashboard是一个数组。 So use Dashboard[0]所以使用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. [ ]用于检索Today's turnover密钥而不是dot(.)符号。 You can check this Link for more information您可以查看此链接以获取更多信息

jsfiddle提琴手

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 ). JavaScript 对象的属性也可以使用括号表示法访问或设置(有关更多详细信息,请参阅属性访问器)。 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 对象的属性:

myCar['make'] = 'Ford';
myCar['model'] = 'Mustang';
myCar['year'] = 1969;

For more, read on at Working with JS Objects .有关更多信息,请继续阅读使用 JS 对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM