1

Apology:

I apologize if this is a really basic question, I've looked around but since I never did get a very solid grounding in Javascript objects, I may be looking for answers in all the wrong places. That, and I don't have a solid enough grasp of Javascript to seperate out what is Javascript in all of the JQuery questions, and I don't want to use JQuery until I have a better understanding of Javascript in general. Thank you.

Problem:

I have a for loop that is going through an array of Google Map markers, to return them to active on the map I have open. The markers still exist somehow in the array because they return randomly when I iterate a couple times through this block of code. It will make it through one or two iterations of the loop, and then end. I added the hasOwnProperty test once I noticed the error, but this isn't skipping past the problem item in the array like I thought it should.

Question:

Why is my for loop skipping over objects in the array that I know are there?

//Code Loop
for (var i in removedMarkerArray)
{
  //test for valid object
  if (!removedMarkerArray[i].hasOwnProperty('title')) continue;
  else alert("You dawg, this stuff passed.");

  //the actual code doing real work, rather than testing.
  if (removedMarkerArray[i].PD == PD)
  {
    removedMarkerArray[i].setMap(map);

    placedMarkerArray.push(removedMarkerArray[i]);
    removedMarkerArray.splice(i, 1);
  }
}

//Example object in the array.
var marker = new google.maps.Marker({
    map: map,
    position: results[0].geometry.location,
    title: location.mouseover,
    PD: location.PD
});

Thank you for reading through my question, and I'm looking forward to any answers.

Matthew_Sp
  • 33
  • 8
  • Please shorten the question and refactor it so there will be a boring, non-related part where you write whatever you want, and then a part containing the real question, so I could focus on that. – vsync Nov 10 '15 at 20:00
  • I've cleaned up the Question section, and broke out a Problem section. Is that explaining the problem that I'd like answered better? – Matthew_Sp Nov 10 '15 at 20:29
  • you should change your `for in` loop to a classic `for` loop as I indicated in my answer below as well as put a `console.log(removedMarkerArray[i])` as the first line in your `for` loop to see what that output is and if you are hitting each expected marker. – Abdullah Rasheed Nov 10 '15 at 20:39
  • I've added the "" console.log(removedMarkerArray[i]) "" to the first line within the for loop. I can clearly see the objects coming through, but only one or two make it to the actual map, rather than all of them. I kept the alert in place, and it's still only coming up twice when I run this code. I don't see any objects I didn't expect in there. – Matthew_Sp Nov 10 '15 at 20:45
  • in the console, when you collapse all of your objects, do they have the `PD` property? the next thing I would do is check to make sure all of those objects are actually hitting the line: `removedMarkerarray[i].setMap(map);` – Abdullah Rasheed Nov 10 '15 at 20:52
  • The objects showing up in the console do have the PD property, it shows up on the first line when I break it out. Also, I'm sorry, only 2 objects are actually showing up on the line, I had clicked through this code twice before. I did drop in the line "" alert("DEBUG " + removedMarkerArray.length); "" and it shows me "DEBUG 4" and "DEBUG 3" when all of the objects in this array should be the same object. Everything I keep running into points out that it breaks out somehow after two iterations. – Matthew_Sp Nov 10 '15 at 22:50

4 Answers4

3

for...in is meant to iterate over the Properties of an Object. Use a regular for loop if its meant to iterate through the indicies of an array.

This answer explains why: https://stackoverflow.com/a/5263872/1385467

Community
  • 1
  • 1
Adam Finley
  • 1,550
  • 1
  • 16
  • 28
  • I did change my code to use a normal for loop instead of the for...in loop. I understand that this is good practice, but it doesn't appear to have fixed my problem. Do you have any other advice? – Matthew_Sp Nov 10 '15 at 20:39
  • I found the actual answer. Thank you for helping me be a better Javascript programmer in the future, but your answer did not help me solve this problem at all. – Matthew_Sp Nov 11 '15 at 15:28
1

If removedMarkerArray is a true Array you should use a classic for loop rather than for in. So you should be doing something of the sort:

for(var i = 0, n = removedMarkerArray.length; i < n; i++){
    console.log(removedMarkerArray[i]); //check desired object in console for debugging
    ..code here
}
Abdullah Rasheed
  • 3,562
  • 4
  • 33
  • 51
  • I updated the question with an example of what I'm putting in the array. Just so I understand, it's an array and not an object? I declared the variable originally with "var removedMarkerArray = [];", and used push to add the objects to the array. – Matthew_Sp Nov 10 '15 at 20:31
  • 1
    Yes, its an array if you declared it with the Array literal syntax, `var removedMarkerArray = []`, but Array's are objects. If you want to iterate through the indices as Adam Pointed out you should use the classic `for-loop` that i posted, but if you want to iterate through the properties you would use `for-in` loop. I'll take a look at the updated question. – Abdullah Rasheed Nov 10 '15 at 20:33
  • @Matthew_Sp if you put `console.log(removedMarkerArray[i]);` in your for loop do you see the desired object each time? – Abdullah Rasheed Nov 10 '15 at 20:43
  • I commented on the original question, because that's where I saw you mention it, and you hadn't updated your question yet. I did add the logging to console command, and I'm getting all of the objects that disappear in another section of code, but not all of the objects are returning to the screen. I am pretty sure it's this chunk of code, because the alert I left in is only iterating twice. – Matthew_Sp Nov 10 '15 at 20:47
  • I found the answer, and I've added it as an answer here. Thank you for your help, it let me to the answer with a clear mind this morning. – Matthew_Sp Nov 11 '15 at 15:29
0

I like forEach for better readability as long as you are using a real Array.

removedMarkerArray.forEach(function(removedMarker, index) {
  //test for valid object
  if (!removedMarker.hasOwnProperty('title')) continue;
  else console.log("You dawg, this stuff passed.");

  //the actual code doing real work, rather than testing.
  if (removedMarker.PD == PD)
  {
    removedMarker.setMap(map);

    removedMarkerArray.splice(index, 1);
    placedMarkerArray.push(removedMarker);
  }
});
pherris
  • 17,195
  • 8
  • 42
  • 58
  • I'm not sure I'm following you. From what I was digging up on Splice, it should be removing just the object I'm working on from the array. Just before that, I'm pushing the marker I'm about to delete to another array so I don't lose it. Is there something else going on in the background I'm not aware of? – Matthew_Sp Nov 10 '15 at 20:35
  • 1
    I missed the different variable name - thinking you were replacing it in the `removedMarkerArray`. Updated answer. – pherris Nov 10 '15 at 20:53
0

The problem with my code removing a marker from the map stems not from the fact that I used for...in instead of a true for loop, which the highest rated question stated, it stemmed from that I was skipping over items in my array. When I was originally writing this, I assumed for...in would protect me from that, but as far as this is concerned, it worked just like the loop below.

I added a decrement on the i variable, so that it would run through the same index again instead of skipping one. This is now touching on all of the markers in my arrays.

for (var i = 0; i < placedMarkerArray.length; i++)
{
  if (placedMarkerArray[i].PD === PD)
  {
    placedMarkerArray[i].setMap(null);

    console.log(placedMarkerArray[i]);

    removedMarkerArray.push(placedMarkerArray[i]);
    placedMarkerArray.splice(i, 1);
>>  i--;
  }
}
Matthew_Sp
  • 33
  • 8