0

I retrieved objects and pushed them into another object and I tried to loop over the new object using the "for in-looop" however it does not loop over the code.

my object

var allControls = {}

How I retrieved data from my firebase database

 var controlRef = database.ref('Controls');
    controlRef.on('value', function(snapshot) {
        snapshot.forEach(function(childSnapshot) {
          var control = childSnapshot.val();
          allControls[control.name] = control;
        _newMarker = L.marker(control.LatLong,
            {title: control.name,
                riseOnHover: true,   
                icon:  markerIcon},).addTo(mymap);

my for loop which does not run

for (var i in allControls){
    console.log(allControls[i])

    var data = allControls[i];
    console.log(data)

      var icon = L.icon({
        //   iconUrl: data.icon,
          iconSize:     [52, 60], // size of the icon
          iconAnchor:   [26, 60], // point of the icon which will correspond to marker's location
          popupAnchor: [0, -60]
      });
      if (data.iconori === "left") {
        icon = L.icon({
        //   iconUrl: data.icon,
          iconSize:     [60, 52], 
          iconAnchor:   [60, 26], 
          popupAnchor: [-35, -26]
          });
      };
      if (data.iconori === "right") {
        icon = L.icon({
        //   iconUrl: data.icon,
          iconSize:     [60, 52], 
          iconAnchor:   [0, 26], 
          popupAnchor: [35, -26]
          })
        };
      }

any ideas why my loop might not run?

kojo justine
  • 103
  • 1
  • 12

1 Answers1

1

I think you meant to use for... of..

What is the difference between ( for... in ) and ( for... of ) statements in JavaScript?

Also, what you get in i is already the data (that's the difference from a regular for loop),

So try something like this:

for (let data of allControls){
    console.log(data);

    //Add your code here
}
audzzy
  • 721
  • 1
  • 4
  • 12