当前位置: 首页 > 面试题库 >

JavaScript如何根据属性过滤对象数组?

边浩漫
2023-03-14
问题内容

我有以下JavaScript数组的房地产主页对象:

var json = {
    'homes': [{
            "home_id": "1",
            "price": "925",
            "sqft": "1100",
            "num_of_beds": "2",
            "num_of_baths": "2.0",
        }, {
            "home_id": "2",
            "price": "1425",
            "sqft": "1900",
            "num_of_beds": "4",
            "num_of_baths": "2.5",
        },
        // ... (more homes) ...     
    ]
}

var xmlhttp = eval('(' + json + ')');
homes = xmlhttp.homes;

我想做的是能够对对象执行过滤,以返回“家庭”对象的子集。

例如,我想根据能够过滤:pricesqftnum_of_beds,和num_of_baths

如何在JavaScript中执行类似下面的伪代码的操作:

var newArray = homes.filter(
    price <= 1000 & 
    sqft >= 500 & 
    num_of_beds >=2 & 
    num_of_baths >= 2.5 );

注意,语法不必与上面完全相同。这只是一个例子。


问题答案:

您可以使用以下Array.prototype.filter方法:

var newArray = homes.filter(function (el) {
  return el.price <= 1000 &&
         el.sqft >= 500 &&
         el.num_of_beds >=2 &&
         el.num_of_baths >= 2.5;
});

现场示例:

var obj = {

    'homes': [{

            "home_id": "1",

            "price": "925",

            "sqft": "1100",

            "num_of_beds": "2",

            "num_of_baths": "2.0",

        }, {

            "home_id": "2",

            "price": "1425",

            "sqft": "1900",

            "num_of_beds": "4",

            "num_of_baths": "2.5",

        },

        // ... (more homes) ...

    ]

};

// (Note that because `price` and such are given as strings in your object,

// the below relies on the fact that <= and >= with a string and number

// will coerce the string to a number before comparing.)

var newArray = obj.homes.filter(function (el) {

  return el.price <= 1000 &&

         el.sqft >= 500 &&

         el.num_of_beds >= 2 &&

         el.num_of_baths >= 1.5; // Changed this so a home would match

});

console.log(newArray);

此方法是新ECMAScript 5th Edition标准的一部分,几乎可以在所有现代浏览器中找到。

对于IE,您可以包括以下兼容性方法:

if (!Array.prototype.filter) {
  Array.prototype.filter = function(fun /*, thisp*/) {
    var len = this.length >>> 0;
    if (typeof fun != "function")
      throw new TypeError();

    var res = [];
    var thisp = arguments[1];
    for (var i = 0; i < len; i++) {
      if (i in this) {
        var val = this[i];
        if (fun.call(thisp, val, i, this))
          res.push(val);
      }
    }
    return res;
  };
}


 类似资料:
  • 问题内容: 我在javascript中有一组对象。内容看起来像这样; 我想保留一些对象并删除其余的对象。如果object属性为2或34,则将保留对象。其他对象被删除。遗嘱的结果看起来像这样; 我正在使用node.js v6.91。 编辑:有人建议我使用过滤器来解决这种问题。欢迎使用过滤器技术的答案。 问题答案: 您可以尝试以下方法1和2: 方法1 :(使用) 注意:这将返回一个新数组,并且不会修改

  • 问题内容: jsfiddle http://jsfiddle.net/KfSBq/ 所谓子对象,是指我用ng-repeat显示的所有对象在其内部都包含一个对象列表,并且我希望根据这些子对象之一的属性进行过滤。 仅此一项就非常简单。我有个对象,每个对象都包含和对象列表: 我按类别显示它们: 我的问题是现在仍然显示不包含任何条目的日常对象。如何实现一种情况,如果过滤使列表为空,则该列表也不显示? 问题

  • 问题内容: 我有两个阵列。我正在用PubSidebar过滤基于groupKey。 如果父母重视:日记或存款或任何价值或角色:公共,我在传递内容数组内的对象时遇到问题。我必须在基于的内容数组中传递值。 如果存在Journals and Deposits,则在内容数组内添加Journals and Deposit数据,包括公共数据。(三个对象) 如果存在Journals,则将Contents数组内的J

  • 问题内容: 如果我有一个对象数组,并且想将Angular模型绑定到基于过滤器的元素之一的属性,该怎么做?我可以用一个具体的例子更好地解释: HTML: 控制器: JSBin:http://jsbin.com/adisax/1/edit 我想将第二个输入过滤为具有’C’等级的主题,但是我不想将模型绑定到该 等级 ;我想将其绑定到等级为“ C”的主题的 标题 。 这可能吗?如果可以,怎么做? 问题答案

  • 问题内容: 我试图在AngularJS中创建一个自定义过滤器,该过滤器将通过特定属性的值过滤对象列表。在这种情况下,我要按“极性”属性(“正”,“中性”,“负”的可能值)进行过滤。 这是我没有过滤器的工作代码: HTML: 这是JSON格式的“ $ scope.tweets”数组: 我可以提出的最佳过滤器如下: 此方法返回一个空数组。从“ console.log(polarity)”语句中不会打印

  • 问题内容: 我有一系列对象,我想知道搜索它的最佳方法。给定以下示例,我如何搜索和?jQuery有什么可以帮助的吗?还是我必须自己蛮力搜​​索? 问题答案: 您可以使用: