【问题标题】:Array/List length is zero but Array is not empty数组/列表长度为零但数组不为空
【发布时间】:2018-06-15 16:13:18
【问题描述】:

当我 console.log 我的数组时,它显示长度为零。我检查了它是一个使用Array.isArray 的数组,它返回true。还打印出数组值以确保它们存在并显示在控制台中:

[]
0: "a"
1: "b"
2: "c"
3: "d"
length: 4
__proto__: Array(0)

我看到 __proto__: Array(0) 并且我假设这意味着它是一个长度为 0 的数组,但是我如何使其长度不为零以便我可以遍历它?

我的一个想法是这个函数可能是异步的,但我不确定如何解决这个问题。

参考代码:

var list=[]
//getting data from a database
snapshot.forEach(function (node) {
   list.push(node.val())
   console.log(node.val()) //values print out
})
console.log(list) //array is length zero

我基本上是想在这段代码之后添加一个 for 循环来读取每个值并将其用于其他用途。但是我无法遍历数组,因为它注册为空。

【问题讨论】:

  • 我也遇到过一次这个问题,但这实际上不是问题。我在代码中的某个时刻拼接了数组。 Array.splice 工作修改了相同的参考。所以请检查您是否正在执行任何操作,例如 .splice 或 .map 将原始数组修改为零长度
  • 我使用的唯一函数是 .push 推送到数组/将元素添加到数组。这会是它的原因吗?否则,我看不到任何其他添加到数组的方法。
  • 能否请您显示更多代码?这会有所帮助
  • 你在推送异步代码吗?控制台保存对数组的实时引用,因此当您第一次记录它时,您会看到初始值,但是当您展开它时,您会看到异步进行的更改。
  • __proto__中显示的长度是原型对象的长度,不是this对象的长度。

标签: javascript arrays vue.js


【解决方案1】:

对于其他面临类似问题的人:

您很可能在异步函数中填充数组。

function asyncFunction(list){
 setTimeout(function(){
    list.push('a');
    list.push('b');
    list.push('c');
    console.log(list.length); // array length is 3 - after two seconds
 }, 2000); // 2 seconds timeout
}

var list=[];
//getting data from a database
asyncFunction(list);
console.log(list.length) //array is length zero - after immediately
console.log(list) // console will show all values if you expand "[]" after two seconds

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,当我记录如下所示的变量快照时,变量在执行后的大部分逻辑之后被记录,因为它稍后可用,这就是无法访问数组值的原因。

    let admins = []
    adminRef.once('value',(snapshot)=>{
       console.log(snapshot);
       snapshot.forEach(data=>{
       admins.push(data.key); 
     })
     })
    

    因此对其进行了更改,以便在快照变量可用时执行下面的逻辑

     adminRef.once('value',(snapshot)=>{
            if(snapshot){
                console.log(snapshot);
                snapshot.forEach(data=>{
                admins.push(data.key); 
            })}
    

    【讨论】:

      【解决方案3】:

      我需要查看更多您的代码,但这是您想要的。

      var arr = [1, 3, 5, 6];
      console.log(arr.length);
      for (var i = 0; i < arr.length; i++){
        console.log(arr[i]);
      }
      

      【讨论】:

      • 我实际上是从var list = [] 开始,然后做一个for循环,将它推向list.push(value),在for循环之后我console.log(list.length)
      • 你的 for 循环是什么样的?
      • 我的回答应该对你有所帮助。我没有看到你的 for 循环,但这看起来像你的问题。
      【解决方案4】:

      __proto__ 不是您的对象,它是原型的访问器。

      Object.prototype__proto__ 属性是访问器属性(a getter 函数和 setter 函数)公开内部 [[Prototype]](一个对象或null)通过它的对象 它被访问了。

      __proto__ 的使用存在争议,不鼓励使用。

      【讨论】:

        【解决方案5】:

        类似的问题,而不是解决方案。

                    printVertices(){ 
                        console.log(Array.isArray(this.vertices), this.vertices)
                        let head = "<head> "  
                                    this.vertices = [] 
         
                        console.log(this.vertices)
                        this.vertices.map(data => head += data.data + " ") 
                        head += "<tail>" 
                        console.log(head)
                    }
        
                    } 
        
                   
        

        你可以从我的终端看到顶部!它显示 Array.isarray(myarray) 是 true 并且 this.vertices 的 console.log 是链表,然后在底部打印一个空数组。然而 this.vertices 将linkedList打印到控制台

        【讨论】:

          猜你喜欢
          • 2021-02-17
          • 2010-09-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-02-26
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多