Teknotica
Teknotica

Reputation: 1136

How to loop through an object of objects?

I'm trying to get some data from an Object that has been retrieved from Firebase using AngularJS.

The object looks like this ("feed_items" in code):

enter image description here

And the first 10 Objects look like:

enter image description here

So I'm trying to do a loop as follows:

function getUniqueFilters(feed_items) {    

    angular.forEach(feed_items, function(value, key){                  
        console.log(value);      
    });
}

The output of that console.log(value) is:

enter image description here

I don't understand why I'm not getting any of the first 10 Objects, which are the ones I'm needing the data from. How do I get those?

Thanks in advance!

Upvotes: 1

Views: 135

Answers (2)

Ilan Frumer
Ilan Frumer

Reputation: 32367

My guess is by the time you use console.log, the request is still not resolved.

Probably firebase populates the object after it gets the data from the server.

console.log is by reference and asynchronous so you still see the data in the console.

I'm not familiar with firebase but you should probably look for a callback / promise.

For testing I recommend halting the execution like so:

console.log(object); 

debugger; // now check inside your console

Upvotes: 1

Stepan Suvorov
Stepan Suvorov

Reputation: 26176

what if you try to do so:

function getUniqueFilters(feed_items) {    

    for(var i=0; i<10; i++){
       console.log(feed_items[i]);      
    }
}

Upvotes: 0

Related Questions