第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何根據(jù)屬性過濾對象數(shù)組?

如何根據(jù)屬性過濾對象數(shù)組?

吃雞游戲 2019-05-28 17:00:51
如何根據(jù)屬性過濾對象數(shù)組?我有以下JavaScript數(shù)組的房地產(chǎn)家庭對象: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;我想要做的是能夠?qū)ο髨?zhí)行過濾器以返回“home”對象的子集。例如,我想基于對能夠過濾:price,sqft,num_of_beds,和num_of_baths。如何在JavaScript中執(zhí)行某些操作,如下面的偽代碼:var newArray = homes.filter(     price <= 1000 &      sqft >= 500 &      num_of_beds >=2 &      num_of_baths >= 2.5 );注意,語法不必與上面完全相同。這只是一個(gè)例子。
查看完整描述

3 回答

?
人到中年有點(diǎn)甜

TA貢獻(xiàn)1895條經(jīng)驗(yàn) 獲得超7個(gè)贊

您可以使用以下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;});

實(shí)例:

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第5版標(biāo)準(zhǔn)的一部分,幾乎可以在所有現(xiàn)代瀏覽器中找到。

對于IE,您可以包含以下方法以實(shí)現(xiàn)兼容性:

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;
  };}


查看完整回答
反對 回復(fù) 2019-05-28
?
慕絲7291255

TA貢獻(xiàn)1859條經(jīng)驗(yàn) 獲得超6個(gè)贊

我更喜歡Underscore框架。它建議對象有許多有用的操作。你的任務(wù):

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

可以覆蓋像:

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

希望它對你有用!


查看完整回答
反對 回復(fù) 2019-05-28
  • 3 回答
  • 0 關(guān)注
  • 1249 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號