Reputation: 129
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.
Upvotes: 8
Views: 26379
Reputation: 1
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!
Upvotes: 0
Reputation: 39
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
Upvotes: 0
Reputation: 48
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);
})}
Upvotes: 1
Reputation: 1601
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
Upvotes: 12
Reputation: 43899
__proto__
is not your object, it is the accessor for the prototype.
The
__proto__
property ofObject.prototype
is an accessor property (a getter function and a setter function) that exposes the internal[[Prototype]]
(either an object ornull
) of the object through which it is accessed.The use of
__proto__
is controversial, and has been discouraged.
Upvotes: 1
Reputation: 1212
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]);
}
Upvotes: 0