8

When I console.log my array it shows that the length is zero. I checked that it was an array using Array.isArray which returned true. Also printed out the array values to make sure they exist and it shows in the console:

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

I see that __proto__: Array(0) and I'm assuming this means it's a 0 length Array but how do I make it non-zero length so that I can iterate through it?

A thought I had is that this function might be asynchronous but I'm not really sure how to work around that.

Code for reference:

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

I'm basically trying to add a for loop after this code to read through each value and use it for something else. However I can't iterate through the array because it registers as empty.

374
  • 129
  • 1
  • 2
  • 9
  • I have also faced this issue once, but that is not actually an issue. I was splicing the array at some point in my code. Array.splice work modifies the same refrence. so please check if you are doing any operation like .splice or .map which is modifying an original array to zero length – simbathesailor Jan 05 '18 at 20:11
  • The only function I am using is .push to push to the array/add elements to the array. Would this be the cause of it? I don't see any other way to add to an array otherwise. – 374 Jan 05 '18 at 20:12
  • could you please show more code ?. that will help – simbathesailor Jan 05 '18 at 20:13
  • Are you pushing in asynchronous code? The console holds a live reference to the array, so when you first log it you see the initial value, but when you expand it you see the changes that were made asynchronously. – Barmar Jan 05 '18 at 20:21
  • 3
    The length shown in `__proto__` is the length of the prototype object, not *this* object. – Barmar Jan 05 '18 at 20:27
  • 1
    `length: 4` shows the length of this array. – Barmar Jan 05 '18 at 20:27
  • I am also getting same error. please anyone help on this. – Mizhar Raja Jul 21 '19 at 02:15

6 Answers6

11

For anyone else who is facing similar issue:

You are very likely populating the array within an asynchronous function.

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
safrazik
  • 1,517
  • 10
  • 14
  • This was very helpful. I suspect in most cases this is the correct answer. – David Bernat Jul 08 '22 at 22:11
  • @DavidBernat actually every single answer is misleading and incorrect except for this one: https://stackoverflow.com/a/48120835/12737879 The `proto` is the property of `Object.prototype` if the OP didn't panicked and tried to iterate over the array (which was his goal) he would been able to do so as OP's post shows `length: 4`. – Aleksandar Dec 02 '22 at 23:24
1

__proto__ is not your object, it is the accessor for the prototype.

The __proto__ property of Object.prototype is an accessor property (a getter function and a setter function) that exposes the internal [[Prototype]] (either an object or null) of the object through which it is accessed.

The use of __proto__ is controversial, and has been discouraged.

Roy J
  • 42,522
  • 10
  • 78
  • 102
1

I had the same problem and when I logged the variable snapshot as shown below the variable was logged after most of the logic after it was executed, since it was available later, that is why the Array values are not accessible.

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

So changed it so that the logic underneath was executed once the snapshot variable was available

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

I'd need to see more of your code but this is something that you'd want.

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

Similar problem and not a solution.

            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)
            }

            } 

           

and as you can see from my terminal look at the top! it shows Array.isarray(myarray) is true and console.log of this.vertices is the linkedlist then at the bottom it prints am empty array. However this.vertices prints the linkedList to the console enter image description here

rTaylor
  • 39
  • 5
0

Today I ran into this problem, if you use Vue2 or Vue3, maybe the field is not responsive, you can use Vue.set(target, key, value) or this.$set(target, key, value) to solve the problem!

  • Your answer could be improved with explanation as to why this solves the issue, and including relevant links to documentation – Harrison Jul 24 '23 at 11:09