本文翻译自: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;    //undefinedsetTimeout(function()
{var a = config.col_id_3;    //voila!}, 100);

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

  1. 服务器日志显示英文,在闪亮的服务器上显示R控制台日志

    irJvV.. 6 那么在您的计算机上可能有更好的方法适用于R&R Shiny和在服务器上运行的R Shiny - >库(log4r) library(log4r) loggerDebu ...

  2. 对象删除某个属性_充分了解JavaScript中【对象】的概念(二)

    点击上方「蓝字」关注我们 之前的文章: 充分了解JavaScript中[对象]的概念(一) 这篇文章我们继续来讲解JavaScript中[对象]的概念,因为这是一系列的文章,所以建议从第一篇文章开始看 ...

  3. Tomcat9 (catalina.bat)控制台日志乱码

    前言 windows server 2003 tomcat 9.0.50 控制台日志乱码 通过startup.bat启动tomcat后,控制台日志中,中文显示乱码. 解决办法 打开tomcat文件夹( ...

  4. javascript原生事件句柄、BOM、DOM对象属性方法总结

    javascript原生事件句柄.BOM.DOM对象属性方法总结 JS事件句柄 事件句柄 类型 说明 onabort 事件句柄 图像加载被中断 onblur 事件句柄 元素失去焦点 onfocus 事 ...

  5. java bean 对象属性复制框架BeanMapping-01-入门案例

    项目简介 Bean-Mapping 用于 java 对象属性赋值. 项目中经常需要将一个对象的属性,赋值到另一个对象中. 常见的工具有很多,但都多少不够简洁,要么不够强大. 特性 支持对象属性的浅拷贝 ...

  6. matlab 对象属性,matlab对象属性

    文本串 TerminalOneWindow no: yes: 由终端图形驱动器使用 终端有多窗口 终端只有一个窗口 *TerminalDimensions 终端尺寸向量[width,height] T ...

  7. java jdk8 使用stream实现两个list集合合并成一个list集合(对象属性的合并)

    java使用stream实现list中对象属性的合并: 根据两个List中的某个相同字段合并成一条List,包含两个List中的字段 目录 一.前言 二.示例 示例1:java8 合并两个 list& ...

  8. java获取系统运行日志文件_java – 如何获取特定的日志文件并在jenkins控制台输出中显示其内容...

    我有以下Jenkins post-build shell脚本: ssh user@my_server < service my_service stop service my_service s ...

  9. javascript控制台_超越控制台日志3种在javascript中格式化控制台输出的方法

    javascript控制台 As JavaScript developers, we intuitively use console.log() to debug, print out variabl ...

最新文章

  1. Visual Studio 2019更新到16.1.4
  2. 【PAT】A1060 Are They Equal *
  3. JS(Javascript)校验表单项的内容是否合规
  4. RocketMQ知识点整理
  5. MATLAB分子微纳模拟,FDTD/MODE/DEVICE/Interconnect 微纳光电子模拟
  6. js 的函数参数的默认值问题
  7. 偏安一隅的健身房和健身器材市场,还有多少故事可讲?
  8. 解决Win10 /Win11 Fastboot驱动问题
  9. matlab 一阶惯性环节,一阶惯性环节
  10. Silvaco TCAD仿真基础2
  11. python爬虫猫眼电影TOP100(爬虫入门基础,同步入库)
  12. 5. find操作详解
  13. 2016苹果开发者账号注册申请流程链接
  14. 网易云信 UI 开发
  15. 计算机毕业设计之 少儿编程学习平台的设计与实现
  16. Android开发实例-Android平台手机新闻客户端
  17. 金融风控实战——信贷特征衍生与筛选(中国移动人群画像赛TOP1)
  18. Impala入门操作
  19. 数据移动指令-----mov,lea,xchg
  20. ubuntu下手动安装gnome插件

热门文章

  1. golang实现AES ECB模式的加密和解密
  2. centos6使用docker部署zookeeper
  3. Qt 遍历目录下所有图片
  4. 【转】移动互联网应用测试成长技能树V1.0
  5. 可在网络不好的环境下运行的ENet示例程序
  6. [JavaScript]高效 JavaScript
  7. AC自动机 HDU 2222
  8. 使用工具连接linux中的mysql8.0
  9. 一步一步安装Git控件版本工具
  10. javascript 模仿 html5 placeholder