{"id":650,"date":"2022-08-30T15:04:53","date_gmt":"2022-08-30T15:04:53","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/each-statement-stops-when-it-gets-to-undefined-value-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:04:53","modified_gmt":"2022-08-30T15:04:53","slug":"each-statement-stops-when-it-gets-to-undefined-value-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/each-statement-stops-when-it-gets-to-undefined-value-collection-of-common-programming-errors\/","title":{"rendered":"$.each statement stops when it gets to &ldquo;undefined&rdquo; value-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m pretty sure the reason you&#8217;re getting an undefined value during iteration when your starting array doesn&#8217;t have any undefined values in it is that you&#8217;re removing items from the array while iterating through it. I guess this confuses the jQuery <code>$.each()<\/code> iterator.<\/p>\n<p>If you look at your output, what is happening is this:<\/p>\n<pre><code>1st Iteration\n    index === 0, array is[\"markerZip02111\", \"markerZip02139\", \"markerZip01002\",\n                          \"markerZip94602\", \"markerZip02460\"]\n    item 0 \"markerZip02111\" gets removed, shifting all the later elements up\n2nd Iteration\n    index === 1, but now array is [\"markerZip02139\", \"markerZip01002\",\n                                   \"markerZip94602\", \"markerZip02460\"]\n    item 1 \"markerZip01002\" gets removed, shifting all the later elements up\n3rd Iteration\n    index ===2, but now array is [\"markerZip01002\", \"markerZip94602\",\n                                  \"markerZip02460\"]\n    so the last item \"markerZip02460\" gets removed\n4th Iteration\n    index === 3, but now array only has two elements so value\n    at that index is undefined.\n<\/code><\/pre>\n<p>Notice that two of the items never got evaluated: the iterator skipped over them because you changed their indexes by removing items.<\/p>\n<p>If you must remove items as you go it is easy with a conventional for loop that iterates backwards such that removing items won&#8217;t screw up the loop counter. (Or you can use a conventional for loop to go forwards as long as you adjust the counter variable each time you remove an item.)<\/p>\n<p>Also, when you do splice you need to pass the index of the item as the first parameter, not the value of the item. So <code>markersArray.splice(index, 1);<\/code> <em>not<\/em> <code>markersArray.splice(value, 1);<\/code>.<\/p>\n<p>So, something like:<\/p>\n<pre><code>function removeAllMarkers(exceptId) {\n   var value;\n   for (var i = markersArray.length - 1; i &gt;= 0; i--) {\n      value = markersArray[i];\n      if (value != exceptId) {\n         markersArray.splice(i, 1);\n         eval(value+\".setMap(null);\");\n         console.log(value + \" removed\");\n      }\n   }\n}\n<\/code><\/pre>\n<p id=\"rop\"><small>Originally posted 2013-11-09 21:10:36. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;m pretty sure the reason you&#8217;re getting an undefined value during iteration when your starting array doesn&#8217;t have any undefined values in it is that you&#8217;re removing items from the array while iterating through it. I guess this confuses the jQuery $.each() iterator. If you look at your output, what is happening is this: 1st [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-650","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/650","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=650"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/650\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=650"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=650"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=650"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}