简体   繁体   English

在控制台中打印数组时长度错误

[英]Getting a wrong length when printing an array in console

When doing a console.log of an array, I get the following results:在对数组进行console.log时,我得到以下结果:

25 != 2727 == 27

What does the number (25) represent in the first picture (at the top)?第一张图片(顶部)中的数字(25)代表什么? My array has a length of 27, not 25. I thought it was the count of non- null values, but the second picture tells otherwise.我的数组的长度为 27,而不是 25。我认为这是非null值的计数,但第二张图片表明并非如此。

The (25) and (27) are also present in the reduced form: (25) 和 (27) 也以简化形式存在:

25 != 2727 == 27

This happens in both Firefox and Chrome.这发生在 Firefox 和 Chrome 中。

The code that leads to this behaviour:导致这种行为的代码:

var lows = ts2values[current_ts];
var highs = ts2values[current_ts];

console.log("lows b : ", lows);
console.log("highs b : ", highs);

lows.push(null);
highs.push(null);

console.log("lows a : ", lows);
console.log("highs a : ", highs);

What does the number (25) represent in the first picture (at the top)?第一张图片(顶部)中的数字(25)代表什么?

The length of the array at that time .当时数组的长度。

You can see the same result by the following code.您可以通过以下代码看到相同的结果。

myArray = [1, 2, 3]
console.log(myArray)

myArray.push(4, 5, 6)
console.log(myArray)

the first log is Array(3)第一个日志是Array(3)

控制台,不展开数组

and expanding the first log, the length of the array is 6, not 3 because it was updated after console.log并扩展第一个日志,数组的长度是6,而不是3,因为它是在console.log之后更新的

控制台,展开数组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM