Chrome开发人员工具阵列长度 [英] Chrome developer tool Array length

查看:106
本文介绍了Chrome开发人员工具阵列长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


图像的第一行应该像Array [9]为什么它显示数组[0]



看起来你是在你添加对象到数组之前记录数组的输出。就像

  var arr = []; 
console.log(arr);
arr.push({}); // push blank obj

在这里,执行这段代码将导致 Array [ 0] 但它确实包含你的对象,并且如果你有返回一个长度 1 尝试 arr.length

如果您有某种将项目推送到数组的异步函数,也会发生这种情况,这会导致同样的情况。例如,

  var a = []; 
setTimeout(function(){
.push({});
.push({});
},1000);
console.log(a);

我认为,这种行为是故意的,Chrome想要证明最初您的Array是空的,稍后,项目会在运行时推送。


Why chrome developer tool showing 0 array length (first line Array[0]) even if there are total 9 objects in it?

At first line of image should be like Array[9] why it is showing Array[0]

In second image array has 13 objects so it is showing Array[13] not Array[0]

解决方案

It seems like you are logging the output of the array before you are adding objects to your array. Something like

var arr = [];
console.log(arr);
arr.push({}); //pushing blank obj

Here, executing this code will result in Array[0] but it does hold your object and will return a length of 1 if you try arr.length.

This might also happen if you are having some sort of Async function which pushes item to your array, which will result in the same thing. For example,

var a = [];
setTimeout(function(){
  a.push({});
  a.push({});
}, 1000);
console.log(a);

I think, this behavior is intentional, where Chrome wants to show that initially, your Array was empty, later on, items were pushed on runtime.

这篇关于Chrome开发人员工具阵列长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆