title: JavaScript: How do I dynamically "filter" my objects
body: <p>How do I user the JavaScript "<code>filter</code>" attribute as filter my JavaScript object?</p>
  
  <p>I've been reading the following <a href="https://stackoverflow.com/questions/1694717/javascript-how-to-create-an-object-and-filter-on-those-attributes">StackOverflow post</a>, and am in a similar situation.</p>
  
  <p>I have the following JavaScript object:</p>
  
  <pre><code>{
  'cars' : 
  [{
  "car_id"          : "1",
  "price"           : "42999",
  "make_id"         : "050",
  "year_built"      : "2007",
  "color_id"        : "832"
  },
  ..........
  ]}
  </code></pre>
  
  <p>I'm using JQuery to display controls to allow people to filter based on: Price, Make, Year Built, Color</p>
  
  <p>Per that other post, I can use the following code:</p>
  
  <pre><code>// if using an old browser, define the 'filter' attribute
  if (!Array.prototype.filter)
  {
    Array.prototype.filter = function(fun /*, thisp*/)
    {
      var len = this.length &gt;&gt;&gt; 0;
      if (typeof fun != "function")
        throw new TypeError();
  
      var res = new Array();
      var thisp = arguments[1];
      for (var i = 0; i &lt; len; i++)
      {
        if (i in this)
        {
          var val = this[i]; // in case fun mutates this
          if (fun.call(thisp, val, i, this))
            res.push(val);
        }
      }
  
      return res;
    };
  }
  </code></pre>
  
  <p>then to perform the actual filter, I can do:</p>
  
  <pre><code>result = cars.
    filter(function(p) { return p.price &gt;= 15000 }).
    filter(function(p) { return p.price &lt;= 40000 }).
    filter(function(p) { return p.year_built &gt;= 2000 }) etc
  </code></pre>
  
  <p><strong>What I don't understand is, how do I use my JQuery controls to dynamically change the <code>filter</code> once the filter has already been set?</strong> Meaning, let's say I have the filter applied from above, <em>then</em> the user changes there mind and wants to increase the maximum they are willing to pay for a car from $40,000 to $50,000. </p>
  
  <p>How would I problematically modify my filter from :</p>
  
  <pre><code>  filter(function(p) { return p.price &lt;= 40000 }).
  </code></pre>
  
  <p>to:</p>
  
  <pre><code>  filter(function(p) { return p.price &lt;= 50000 }).
  </code></pre>