JavaScript:我如何动态地“过滤"?我的对象 [英] JavaScript: How do I dynamically "filter" my objects

查看:27
本文介绍了JavaScript:我如何动态地“过滤"?我的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 JavaScript 的filter"属性来过滤我的 JavaScript 对象?

How do I user the JavaScript "filter" attribute as filter my JavaScript object?

我一直在阅读以下StackOverflow 帖子,我也遇到了类似的情况.

I've been reading the following StackOverflow post, and am in a similar situation.

我有以下 JavaScript 对象:

I have the following JavaScript object:

{
'cars' : 
[{
"car_id"          : "1",
"price"           : "42999",
"make_id"         : "050",
"year_built"      : "2007",
"color_id"        : "832"
},
..........
]}

我使用 JQuery 来显示控件以允许人们根据以下条件进行过滤:价格、制造商、建造年份、颜色

I'm using JQuery to display controls to allow people to filter based on: Price, Make, Year Built, Color

根据其他帖子,我可以使用以下代码:

Per that other post, I can use the following code:

// if using an old browser, define the 'filter' attribute
if (!Array.prototype.filter)
{
  Array.prototype.filter = function(fun /*, thisp*/)
  {
    var len = this.length >>> 0;
    if (typeof fun != "function")
      throw new TypeError();

    var res = new Array();
    var thisp = arguments[1];
    for (var i = 0; i < 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;
  };
}

然后执行实际过滤器,我可以这样做:

then to perform the actual filter, I can do:

result = cars.
  filter(function(p) { return p.price >= 15000 }).
  filter(function(p) { return p.price <= 40000 }).
  filter(function(p) { return p.year_built >= 2000 }) etc

我不明白的是,一旦设置了过滤器,我如何使用我的 JQuery 控件来动态更改 filter? 意思是,假设我如果从上面应用过滤器,然后用户会改变主意并希望将他们愿意为汽车支付的最高金额从 40,000 美元增加到 50,000 美元.

What I don't understand is, how do I use my JQuery controls to dynamically change the filter once the filter has already been set? Meaning, let's say I have the filter applied from above, then 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.

我将如何有问题地修改我的过滤器:

How would I problematically modify my filter from :

  filter(function(p) { return p.price <= 40000 }).

到:

  filter(function(p) { return p.price <= 50000 }).

推荐答案

一旦设置了过滤器,我如何使用我的 JQuery 控件来动态更改过滤器?

how do I use my JQuery controls to dynamically change the filter once the filter has already been set?

您没有设置过滤器.您使用过滤器函数调用 filter() 并返回过滤后的数组;您无法更改之后应用于数组的过滤器.相反,您必须再次调用 filter(),并传递不同的过滤器函数.

You don't set a filter. You call filter() with a filter function and get a filtered array back; you can't change the filter that was applied to the array afterwards. Instead you must call filter() again, and pass a different filter function.

或者同一个过滤器函数,对一个已经改变的变量进行闭包:

Or the same filter function with a closure over a variable that has changed:

var minprice= 10000;
var minpricefilter= function(p) { return p.price>=minprice };
result= cars.filter(minpricefilter);

minprice= 20000;
result= cars.filter(minpricefilter);

这篇关于JavaScript:我如何动态地“过滤"?我的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆