Array has records but showing array length 0 in console

前端 未结 1 1344
独厮守ぢ
独厮守ぢ 2020-12-20 08:16

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.

<
相关标签:
1条回答
  • 2020-12-20 09:05

    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 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 讨论(0)
提交回复
热议问题