数组有记录,但在控制台中显示数组长度 0 [英] Array has records but showing array length 0 in console

查看:22
本文介绍了数组有记录,但在控制台中显示数组长度 0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码中有文件数组.当我添加新文件时,它显示的数组长度不正确.但是,如果我用 settimeout 检查长度,它会显示正确的长度.

There is files array in my code. When i adds new file, its showing incorrect length of array. But, if i checked the length with settimeout its showing correct length.

console.log('row.myDocuments: ', row.myDocuments);  
console.log('length: ', row.myDocuments.length);

像这样在控制台中获取结果,

Getting results in console like this,

每次添加新文件时,进入控制台的长度都是(实际长度-1)

Every time when i add new file, the length getting in console is (actual length -1)

推荐答案

这和 Chrome 开发工具有关.你看到[]"旁边蓝色方块中的小i"了吗?如果你用鼠标悬停它,它会显示刚刚评估了下面的值".这意味着在您记录您的数组(实际上是一个 javascript 对象)时,它是空的,但后来 Chrome 检测到了更改,并在控制台中更新了该数组.

This is related to Chrome dev tools. You see the little "i" in a blue square next to "[]"? If you hover it with your mouse, it says "value below was evaluated just now". It means that at the time you logged your array (which is actually a javascript Object), it was empty, but later Chrome detected a change, and updated the array in the console.

(只有当你使用小箭头展开它时,你才能看到数组在记录时是空的,因为它看起来像这样[]",否则它看起来像这样[{...}]").

(And that is only if you expand it using the little arrow, you can see the array was empty at the time it was logged since it looks like this "[]", otherwise it would have looked like this "[{...}]").

Chrome 会在您记录对象时更新对象,但不会更新简单值.Array.length 是一个数字,所以当你记录它时,它会给你它的值(0),如果 Array.length 以后发生变化,它不会更新控制台;但如果您在更改后记录它(与您的超时一样),它将记录更新的值.

Chrome updates object when you log them, but not simple values. Array.length is a number, so when you log it, it gives you its value (0), and if Array.length changes later, it will not update the console; but if you log it after the change (as with your timeout), it will log the more recent value.

这篇关于数组有记录,但在控制台中显示数组长度 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆