JS长度短于元素数

时间:2019-06-20 15:13:31

标签: javascript arrays

我有一个长度为20的JS数组,但是.length,一个forEach和一个for循环都说基本上为11。

我尝试通过某种方式计算它(以.length开始):

    console.log('data length: ' + data.length);
    var myLen = 0;
    myLen = Object.keys(data).length;
    console.log( 'myLen: ' + myLen);
    for (var i = 0, len = 0; i < data.length; i++, data[i] !== undefined && len++);
    console.log('real length: ' + len );

以上代码的输出:

数据长度:11 myLen:11 实际长度:10


Chrome开发者工具控制台中的数组:

(11) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {b_id: "3759", start_date: "2019-06-01", end_date: "2019-06-30", amount: "1800", platform_id_fk: "1", …}
1: {b_id: "3759", start_date: "2019-06-01", end_date: "2019-06-30", amount: "1800", platform_id_fk: "1", …}
2: {b_id: "3759", start_date: "2019-06-01", end_date: "2019-06-30", amount: "1800", platform_id_fk: "1", …}
3: {b_id: "3759", start_date: "2019-06-01", end_date: "2019-06-30", amount: "1800", platform_id_fk: "1", …}
4: {b_id: "3759", start_date: "2019-06-01", end_date: "2019-06-30", amount: "1800", platform_id_fk: "1", …}
5: {b_id: "3759", start_date: "2019-06-01", end_date: "2019-06-30", amount: "1800", platform_id_fk: "1", …}
6: {b_id: "3759", start_date: "2019-06-01", end_date: "2019-06-30", amount: "1800", platform_id_fk: "1", …}
7: {b_id: "3759", start_date: "2019-06-01", end_date: "2019-06-30", amount: "1800", platform_id_fk: "1", …}
8: {b_id: "3759", start_date: "2019-06-01", end_date: "2019-06-30", amount: "1800", platform_id_fk: "1", …}
9: {b_id: "3759", start_date: "2019-06-01", end_date: "2019-06-30", amount: "1800", platform_id_fk: "1", …}
10: {b_id: "3759", start_date: "2019-06-01", end_date: "2019-06-30", amount: "1800", platform_id_fk: "1", …}
11: {amount: "1800", b_id: "3759", campaignCount: 0, campaigns: 1, company_id_fk: "596", …}
12: {amount: "1800", b_id: "3759", campaignCount: 0, campaigns: 1, company_id_fk: "596", …}
13: {amount: "1800", b_id: "3759", campaignCount: 0, campaigns: 1, company_id_fk: "596", …}
14: {amount: "1800", b_id: "3759", campaignCount: 0, campaigns: 1, company_id_fk: "596", …}
15: {amount: "1800", b_id: "3759", campaignCount: 0, campaigns: 1, company_id_fk: "596", …}
16: {amount: "1800", b_id: "3759", campaignCount: 0, campaigns: 1, company_id_fk: "596", …}
17: {amount: "1800", b_id: "3759", campaignCount: 0, campaigns: 1, company_id_fk: "596", …}
18: {amount: "1800", b_id: "3759", campaignCount: 0, campaigns: 1, company_id_fk: "596", …}
19: {amount: "1800", b_id: "3759", campaignCount: 0, campaigns: 1, company_id_fk: "596", …}
length: 20
__proto__: Array(0)

此后,我尝试遍历此数组,当然,现在计数已关闭。为什么说10/11而不是20?

1 个答案:

答案 0 :(得分:5)

Chrome(和Firefox)开发工具在您致电console.log()时记录日志。对于对象,它记录对象的“摘要版本”。但是,当您扩展记录的对象时,扩展版本的内容将在扩展对象时显示对象的内容。

简而言之,在您记录日志时,它是 11。但是在您扩展日志条目时,它已经是20。在记录日志和扩展它之间,有一些代码可能还会添加9个项目。

这是一个简单的演示。 (在实际的浏览器控制台中查看,因为SO片段会序列化输出以进行显示):

const array = [1, 2, 3]

// This will show 3
console.log('length: ', array.length)

// This will show 3 items
console.log('serialized contents', JSON.stringify(array)) 

// This will show 3 items unexpanded, and is 3 at the time it was logged.
// But this will show 12 upon expand
console.log('unserialized contents', array) 

// Pop in more items
array.push(4, 5, 6, 7, 8, 9, 10, 11, 12)

一种解决方法是在对象上使用JSON.stringify()。将对象序列化为字符串,然后记录该字符串。或者,使用开发工具断点来调试代码,而不是console.log()