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.

7
  • 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 Commented Jan 5, 2018 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. Commented Jan 5, 2018 at 20:12
  • could you please show more code ?. that will help Commented Jan 5, 2018 at 20:13
  • 3
    The length shown in __proto__ is the length of the prototype object, not this object. Commented Jan 5, 2018 at 20:27
  • 1
    length: 4 shows the length of this array. Commented Jan 5, 2018 at 20:27

6 Answers 6

13

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
Sign up to request clarification or add additional context in comments.

2 Comments

This was very helpful. I suspect in most cases this is the correct answer.
@DavidBernat actually every single answer is misleading and incorrect except for this one: 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.
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.

Comments

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

Comments

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

3 Comments

I actually start off with var list = [] and do a for-loop that pushes to it list.push(value) and after the for-loop I console.log(list.length)
What's your for loop look like?
My answer should help you. I haven't seen your for loop but that looks like your issue.
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

Comments

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!

1 Comment

Your answer could be improved with explanation as to why this solves the issue, and including relevant links to documentation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.