title: Delete value from array object in javascript (jquery)
body: <p>I am trying to delete objects from my array via javascript. Im building a simple mobile jquery application and want to delete items from an array. I use the following code. I have a list with checkboxes in it. Every checkbox has a value that belongs to the list item. So when multiple checkboxes are checked.. it should delete all these objects (items) from the array.</p>
  
  <pre><code>function deleteFunction()
      {
          objects = getObjects();
          $("input:checked").each(function()
          {
              var inputValue = $(this).val();
              for(i = getObjects().length; i &gt;=0; i--)
              {
  
                  if('{"title":"'+ inputValue + '"}' == JSON.stringify(objects[i]))
                  {
                      objects.splice(i, 1);
                      return true;
                  }
              }
  
          });
          alert(JSON.stringify(objects));
          window.location.reload();
      }
  </code></pre>
  
  <p>The annoying thing is as follows:
  When i slice the object from the array, the object is restored on the second iteration. So it always removes only 1 object from the array.</p>
  
  <p>To test my output i used the following code within the if statement:</p>
  
  <pre><code>alert(i);
  alert(JSON.stringify(objects[i]));  
  objects.splice(i, 1);
  alert(i);
  alert(JSON.stringify(objects)); 
  return true;    
  </code></pre>
  
  <p>The output is as follows</p>
  
  <pre><code>1
  {"title":"hi2"}
  1
  [{"title":"hi1"}, {"title":"hi3"}]
  2
  {"title":"hi3"}
  2
  [{"title":"hi1"}, {"title":"hi2"}]
  </code></pre>
  
  <p>so i slice hi2, but has returned </p>
  
  <p>Thanx for answer and respond</p>
  
  <p><strong>Solution Thanx to depperm + indubitablee:</strong></p>
  
  <pre><code>function deleteFunction()
  {
      var objects = getObjects();
      $("input:checked").each(function()
      {
          var inputValue = $(this).val();
          for(i = objects.length -1; i &gt;=0; i--)
          {           
              if(objects[i].title == inputValue)
              {                               
                  objects.splice(i, 1);           
              }
          }
  
      });
      localStorage.setItem("objects", JSON.stringify(objects));
      window.location.reload();
  }
  </code></pre>