数组有记录,但在控制台中显示数组长度为0 [英] Array has records but showing array length 0 in console
问题描述
我的代码中有文件数组.当我添加新文件时,其显示的数组长度不正确.但是,如果我用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屋!