Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

angular - Array has records but showing array length 0 in console

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,

enter image description here

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...