{"id":6235,"date":"2014-04-14T03:21:40","date_gmt":"2014-04-14T03:21:40","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/14\/problem-with-breaking-out-jquery-each-loop-collection-of-common-programming-errors\/"},"modified":"2014-04-14T03:21:40","modified_gmt":"2014-04-14T03:21:40","slug":"problem-with-breaking-out-jquery-each-loop-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/14\/problem-with-breaking-out-jquery-each-loop-collection-of-common-programming-errors\/","title":{"rendered":"Problem with breaking out Jquery each loop?-Collection of common programming errors"},"content":{"rendered":"<p>What you are doing:<\/p>\n<pre><code>for every existing row R\n  if R.id == newRow.id\n    alert\n    break\n  else\n    add newRow\n<\/code><\/pre>\n<p>This will add the newRow for each row that comes before an existing row with that id. With rows [1,2,3,4,5,6,7,8,9] and adding a row 9 will add that row 8 times before alerting &#8220;already exists&#8221;.<\/p>\n<p>What you mean to do is:<\/p>\n<pre><code>exists = false\nfor every existing row R\n  if R.id == newRow.id\n    existing = true\n    alert\n    break\n\nif !exists\n  add newRow\n<\/code><\/pre>\n<p>Equivalent in JS:<\/p>\n<pre><code>addToTable = function() {\n  var selected = $(\"select[name*='mySelect'] option:selected\").val();\n  var exists = false;\n  $('#myTable').find('tr').each(function() {\n    if ($(this).attr('id')==selected) { \n      alert('Record has already existed!');\n      exists = true;\n      return false;\n    }\n  });\n  if(!exists) {\n    $('#favourite_hotels_table').append('<\/code><\/pre>\n<tr id=\"'+selected+'\">\n<td><code>'+selected+'<\/code><\/td>\n<\/tr>\n<p>&#8216;); } }<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What you are doing: for every existing row R if R.id == newRow.id alert break else add newRow This will add the newRow for each row that comes before an existing row with that id. With rows [1,2,3,4,5,6,7,8,9] and adding a row 9 will add that row 8 times before alerting &#8220;already exists&#8221;. What you [&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-6235","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6235","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=6235"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6235\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=6235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=6235"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=6235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}