How do I loop through the children of a Firebase instance

Lango

I want to know how to loop through the children of everyone. I'm using Firebase and AngularJS.

My firebase object looks like:

Firebase Objects

To me it looks like a dictionary, so from Getting a list of associative array keys I have tried

syncData('everyone').$bind($scope, 'everyone').then(function() {
  var keys = $scope.everyone.$getIndex();
  for (var key in $scope.everyone) {
   console.log("key : " + key + " value : " + $scope.everyone[key]);
  }
});

The log does contain the child objects, but it also includes all the methods. Like so

... Before this line is all the other methods.
key : $on value : function (a,c){if("loaded"==a&&b._loaded)return b._timeout(function(){c()}),void 0;if(!b._on.hasOwnProperty(a))throw new Error("Invalid event type "+a+" specified");b._on[a].push(c)} controllers.js:58
key : $off value : function (a,c){if(b._on.hasOwnProperty(a))if(c){var d=b._on[a].indexOf(c);-1!==d&&b._on[a].splice(d,1)}else b._on[a]=[];else b._fRef.off()} controllers.js:58
key : $auth value : function (a){var c=b._q.defer();return b._fRef.auth(a,function(a,b){null!==a?c.reject(a):c.resolve(b)},function(a){c.reject(a)}),c.promise} controllers.js:58
key : $getIndex value : function (){return angular.copy(b._index)} controllers.js:58
key : -JH45WOOAtnZfUZkrJb1 value : [object Object] controllers.js:58
key : -JH45YdfwptGv3y6UqyV value : [object Object] controllers.js:58
key : -JH45_zxptV_dmibyGzL value : [object Object] 

Is there a way I can get just the children?

I'm doing this because my code was designed to use an array, but Firebase discourage using arrays (for values that multiple people could change). So I'm trying to loop through the firebase dictionary and copy the objects into an array on the client side. So I don't have to change too much of my code.

Kato

UPDATE: As of AngularFire 0.8.x, one can use $asArray() to obtain a sorted array of the records and this answer is no longer necessary

The correct way to iterate values in an angularFire object is by using $getIndex(). You have this in your code above, but did not utilize it in the for loop.

Since you are already using the angularFire lib (syncData is the angularFire-seed service that uses angularFire), there is no need to worry about calling $apply() or any of the other complexities of coaxing data into Angular detailed in the previous answer (which is a good response for a raw Firebase/Angular implementation).

Instead, just change your for loop to iterate the keys instead of the angularFire instance:

syncData('everyone').$bind($scope, 'everyone').then(function() {
  var keys = $scope.everyone.$getIndex();

  // utilizing Angular's helpers
  angular.forEach(keys, function(key) {
     console.log(key, $scope.everyone[key]);
  });

  // or as a for loop
  for(var i=0, len = keys.length; i < len; i++) {
     console.log(keys[i], $scope.everyone[keys[i]]);
  }
});

To utilize the object in the DOM, use ng-repeat:

<ul>
  <li ng-repeat="(key,user) in everyone">{{key}}, {{user|json}}</li>
</ul>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I loop all Firebase children in React Native?

From Dev

How do I loop through functions with a for loop?

From Dev

How do I loop through and get all the keys of the nested nodes in firebase?

From Dev

Firebase for Android, How can I loop through a child (for each child = x do y)

From Dev

How do I loop through an xmlhttprequest in javascript

From Dev

How do I reverse a loop halfway through?

From Dev

How do I loop through a JSON array?

From Dev

How do I loop through a list?

From Dev

How do I reverse a loop halfway through?

From Dev

How do I loop through several charges?

From Dev

How do I loop through this JSON object?

From Dev

How do I loop through grid properly?

From Dev

In Firebase, how do I grab the first children of a snapshot?

From Dev

how to loop through "ul" children using JQuery?

From Dev

How do I "loop" through JSON array inside foreach loop?

From Dev

How do I loop through this array with a foreach loop?

From Dev

How do I loop through this until I am out of arrows

From Dev

How can I iterate through children of multiple push keys in Firebase Realtime Database?

From Dev

How do i get all children (ie children of children) with Javascript?

From Dev

How do I create a new project in Firebase through API?

From Dev

Dialogflow: How do I pass a parameter through in a Firebase query?

From Java

How do I loop through an array and stop at a certain index

From Dev

JavaScript: how do I loop through these items outside of their function?

From Java

How do I loop through or enumerate a JavaScript object?

From Dev

How do I loop through objects and categorize by timestamps in Javascript?

From Dev

How do I build a dynamic array and loop through it in AutoIt?

From Dev

How do I loop through this array and call value/s?

From Dev

How do I loop through the mysql resultset in fatfree framework?

From Dev

How do I loop through all compositions in an After Effects project?

Related Related

  1. 1

    How do I loop all Firebase children in React Native?

  2. 2

    How do I loop through functions with a for loop?

  3. 3

    How do I loop through and get all the keys of the nested nodes in firebase?

  4. 4

    Firebase for Android, How can I loop through a child (for each child = x do y)

  5. 5

    How do I loop through an xmlhttprequest in javascript

  6. 6

    How do I reverse a loop halfway through?

  7. 7

    How do I loop through a JSON array?

  8. 8

    How do I loop through a list?

  9. 9

    How do I reverse a loop halfway through?

  10. 10

    How do I loop through several charges?

  11. 11

    How do I loop through this JSON object?

  12. 12

    How do I loop through grid properly?

  13. 13

    In Firebase, how do I grab the first children of a snapshot?

  14. 14

    how to loop through "ul" children using JQuery?

  15. 15

    How do I "loop" through JSON array inside foreach loop?

  16. 16

    How do I loop through this array with a foreach loop?

  17. 17

    How do I loop through this until I am out of arrows

  18. 18

    How can I iterate through children of multiple push keys in Firebase Realtime Database?

  19. 19

    How do i get all children (ie children of children) with Javascript?

  20. 20

    How do I create a new project in Firebase through API?

  21. 21

    Dialogflow: How do I pass a parameter through in a Firebase query?

  22. 22

    How do I loop through an array and stop at a certain index

  23. 23

    JavaScript: how do I loop through these items outside of their function?

  24. 24

    How do I loop through or enumerate a JavaScript object?

  25. 25

    How do I loop through objects and categorize by timestamps in Javascript?

  26. 26

    How do I build a dynamic array and loop through it in AutoIt?

  27. 27

    How do I loop through this array and call value/s?

  28. 28

    How do I loop through the mysql resultset in fatfree framework?

  29. 29

    How do I loop through all compositions in an After Effects project?

HotTag

Archive