Home > other >  Variable the same value both inside and outside for loop
Variable the same value both inside and outside for loop

Time:04-08

I have an array of objects that feed info to a variable that generates "markers":

var markers = [

//testPOW
{ mesh: ["campDome","POW"],
  color: iconRed,
  location: {lat: 43.30985465943113, lng: 11.898409657895801},
  visible: true,
  id: "TestPOW",
  category: "POW"
},

//testCamps
{ mesh: ["campDome","Camps"],
  color: iconBlue,
  location: {lat: 27.051081239338288, lng: 21.074973750899662},
  visible: true,
  id: "TestCamp",
  category: "Camps"
},

//testInternedCivilians
{ mesh: ["campDome","InternedCivilians"],
  color: iconYellow,
  location: {lat: 47.866084023826794, lng: 2.61794289751219},
  visible: true,
  id: "TestInternedCivilians",
  category: "InternedCivilians"
},
]

The following code generates the markers: //Generate Markers

for ( var i=0; i < markers.length; i   ) {

    var marker = this.addMarker( {          
        mesh : markers[i].mesh,
        color: markers[i].color,
        color2: 'white',
        location : markers[i].location,
        scale: 0.4,
        offset: 0,
        visible: markers[i].visible,
        id: markers[i].id,
        category: markers[i].category
        
    } );
};

While not inside the for loop that generates the markers, the variable marker is this:

Earth.Marker {options: {…}, object3d: Vn, isMarker: true, earth: Earth, mesh: Array(2), …}

Which is just the last one to be generated

While inside the loop it's like this:

scripts.js:244 
Earth.Marker {options: {…}, object3d: Vn, isMarker: true, earth: Earth, mesh: Array(2), …}
scripts.js:244 
Earth.Marker {options: {…}, object3d: Vn, isMarker: true, earth: Earth, mesh: Array(2), …}
scripts.js:244 
Earth.Marker {options: {…}, object3d: Vn, isMarker: true, earth: Earth, mesh: Array(2), …}
scripts.js:244 
Earth.Marker {options: {…}, object3d: Vn, isMarker: true, earth: Earth, mesh: Array(2), …}
scripts.js:244 
Earth.Marker {options: {…}, object3d: Vn, isMarker: true, earth: Earth, mesh: Array(2), …}
scripts.js:244 
Earth.Marker {options: {…}, object3d: Vn, isMarker: true, earth: Earth, mesh: Array(2), …}
scripts.js:244 
Earth.Marker {options: {…}, object3d: Vn, isMarker: true, earth: Earth, mesh: Array(2), …}
scripts.js:244 
Earth.Marker {options: {…}, object3d: Vn, isMarker: true, earth: Earth, mesh: Array(2), …}
scripts.js:244 
Earth.Marker {options: {…}, object3d: Vn, isMarker: true, earth: Earth, mesh: Array(2), …}
scripts.js:244 
Earth.Marker {options: {…}, object3d: Vn, isMarker: true, earth: Earth, mesh: Array(2), …}
scripts.js:244 
Earth.Marker {options: {…}, object3d: Vn, isMarker: true, earth: Earth, mesh: Array(2), …}
scripts.js:244 
Earth.Marker {options: {…}, object3d: Vn, isMarker: true, earth: Earth, mesh: Array(2), …}
scripts.js:244 
Earth.Marker {options: {…}, object3d: Vn, isMarker: true, earth: Earth, mesh: Array(2), …}
scripts.js:244 
Earth.Marker {options: {…}, object3d: Vn, isMarker: true, earth: Earth, mesh: Array(2), …}
scripts.js:244 
Earth.Marker {options: {…}, object3d: Vn, isMarker: true, earth: Earth, mesh: Array(2), …}
scripts.js:244 
Earth.Marker {options: {…}, object3d: Vn, isMarker: true, earth: Earth, mesh: Array(2), …}

How can I get it marker to include all of the generated objects if called outside the for loop?

CodePudding user response:

Does creating an array help?

let marker = [];

for (var i=0; i < markers.length; i  ) {

    marker.push(this.addMarker( {          
        mesh : markers[i].mesh,
        color: markers[i].color,
        color2: 'white',
        location : markers[i].location,
        scale: 0.4,
        offset: 0,
        visible: markers[i].visible,
        id: markers[i].id,
        category: markers[i].category
        
    }));
};

or, may be an object?


let marker = {};

for (var i=0; i < markers.length; i  ) {

    marker[markers[i].id] = this.addMarker({          
        mesh : markers[i].mesh,
        color: markers[i].color,
        color2: 'white',
        location : markers[i].location,
        scale: 0.4,
        offset: 0,
        visible: markers[i].visible,
        category: markers[i].category
        
    });
};

  • Related