Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array/List length is zero but Array is not empty

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.

like image 790
374 Avatar asked Jan 05 '18 20:01

374


People also ask

Does an empty array have a length of 0?

The length property sets or returns the number of elements in an array. By knowing the number of elements in the array, you can tell if it is empty or not. An empty array will have 0 elements inside of it.

Is array length same as zero length?

Is there any difference between checking an array's length as a truthy value vs checking that it's > 0? Since the value of arr. length can only be 0 or larger and since 0 is the only number that evaluates to false , there is no difference.

How do you check if an array is empty or not?

The array can be checked if it is empty by using the array. length property. This property returns the number of elements in the array. If the number is greater than 0, it evaluates to true.

Is array length 0 Falsy?

If the length of the object is 0, then the array is considered to be empty and the function will return TRUE. Else the array is not empty and the function will return False.


2 Answers

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
like image 153
safrazik Avatar answered Oct 02 '22 17:10

safrazik


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); 
        })}
like image 28
Calvin Mwadime Avatar answered Oct 02 '22 18:10

Calvin Mwadime