即使对象属性显示在控制台日志中,也无法访问

本文翻译自:Can't access object property, even though it shows up in a console log

Below, you can see the output from these two logs. 在下面,您可以看到这两个日志的输出。 The first clearly shows the full object with the property I'm trying to access, but on the very next line of code, I can't access it with config.col_id_3 (see the "undefined" in the screenshot?). 第一个清楚地显示了具有我要访问的属性的完整对象,但是在下一行代码中,我无法使用config.col_id_3访问它(请参见屏幕快照中的“ undefined”?)。 Can anyone explain this? 谁能解释一下? I can get access to every other property except field_id_4 as well. 我也可以访问除field_id_4以外的所有其他属性。

console.log(config);
console.log(config.col_id_3);

This is what these lines print in Console 这是这些行在控制台中打印的内容

控制台输出


#1楼

参考:https://stackoom.com/question/1BclN/即使对象属性显示在控制台日志中-也无法访问


#2楼

The output of console.log(anObject) is misleading; console.log(anObject)的输出具有误导性; the state of the object displayed is only resolved when you expand the > in the console. 仅当您在控制台中展开>时,才能解决所显示对象的状态。 It is not the state of the object when you console.log 'd the object. 当您console.log对象时,它不是对象的状态。

Instead, try console.log(Object.keys(config)) , or even console.log(JSON.stringify(config)) and you will see the keys, or the state of the object at the time you called console.log . 相反,请尝试console.log(Object.keys(config)) ,甚至可以是console.log(JSON.stringify(config)) ,您会在调用console.log时看到键或对象的状态。

You will (usually) find the keys are being added after your console.log call. 您通常会 console.log调用之后找到要添加的密钥。


#3楼

Check if inside the object there's an array of objects. 检查对象内部是否有对象数组。 I had a similar issue with a JSON: JSON也有类似的问题:

    "terms": {
        "category": [
            {
                "ID": 4,
                "name": "Cirugia",
                "slug": "cirugia",
                "description": "",
                "taxonomy": "category",
                "parent": null,
                "count": 68,
                "link": "http://distritocuatro.mx/enarm/category/cirugia/"
            }
        ]
    }

I tried to access the 'name' key from 'category' and I got the undefined error, because I was using: 我尝试从“类别”访问“名称”键,但出现未定义的错误,因为我正在使用:

var_name = obj_array.terms.category.name

Then I realised it has got square brackets, that means that it has an array of objects inside the category key, because it can have more than one category object. 然后我意识到它有方括号,这意味着它在类别键内有一组对象,因为它可以有多个类别对象。 So, in order to get the 'name' key I used this: 因此,为了获得“名称”键,我使用了以下命令:

var_name = obj_array.terms.category[0].name

And That does the trick. 这就是窍门。

Maybe it's too late for this answer, but I hope someone with the same problem will find this as I did before finding the Solution :) 也许现在回答这个问题为时已晚,但我希望遇到同样问题的人能像在找到解决方案之前一样找到它:


#4楼

I struggled with this issue today, and thought I'll leave a reply with my solution. 我今天在这个问题上苦苦挣扎,以为我会在解决方案中留下一个答复。

I was fetching a data object via ajax, something like this: {"constants": {"value1":"x","value2":"y"},"i18n" {"data1":"x", "data2":"y"}} 我正在通过ajax来获取数据对象,如下所示: {"constants": {"value1":"x","value2":"y"},"i18n" {"data1":"x", "data2":"y"}}

Let's say this object is in a variable called data. 假设此对象位于名为data的变量中。 Whenever I referenced data.i18n I got undefined . 每当引用data.i18n我都undefined

  1. console.log(data) showed the object as expected console.log(data)显示了预期的对象
  2. console.log(Object.keys(data)) said ["constants","i18n"] as expected console.log(Object.keys(data))如预期般表示["constants","i18n"]
  3. Renaming i18n to inter didn't change anything i18n重命名为inter并没有任何改变
  4. I even tried to switch the data to make "i18n" the first object 我什至尝试切换数据以使“ i18n”成为第一个对象
  5. Moved code around to make absolutely sure the object was completely set and there was no problem with the ajax promise. 移动代码以完全确保对象已完全设置,并且ajax promise没有问题。

Nothing helped... Then on the server side I wrote the data to the php log, and it revealed this: 没有任何帮助...然后在服务器端,我将数据写入了php日志,并显示了这一点:

{"constants": {"value1":"x","value2":"y"},"\і18n" {"data1":"x", "data2":"y"}}

The "i" in the index key was actually a u0456 (cyrillic i). 索引键中的“ i”实际上是u0456(西里尔字母i)。 This was not visible in my php editor or the browser console log. 这在我的PHP编辑器或浏览器控制台日志中不可见。 Only the php log revealed this... That was a tricky one... 只有php日志显示了这一点……那是一个棘手的问题……


#5楼

I had the same issue. 我遇到过同样的问题。 Solution for me was using the stringified output as input to parsing the JSON. 对我来说,解决方案是将字符串化的输出用作解析JSON的输入。 this worked for me. 这对我有用。 hope its useful to you 希望它对您有用

var x =JSON.parse(JSON.stringify(obj));
console.log(x.property_actually_now_defined);

#6楼

The property you're trying to access might not exist yet. 您尝试访问的属性可能尚不存在。 Console.log works because it executes after a small delay, but that isn't the case for the rest of your code. Console.log之所以有效,是因为它会在短暂的延迟后执行,但其余代码并非如此。 Try this: 尝试这个:

var a = config.col_id_3;    //undefined

setTimeout(function()
{
    var a = config.col_id_3;    //voila!

}, 100);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值