国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

根據(jù)屬性過濾對象數(shù)組:指南
P粉087951442
P粉087951442 2023-10-09 16:41:54
0
2
866

我有以下房地產(chǎn)家庭對象的 JavaScript 數(shù)組:

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”對象的子集。

例如,我希望能夠基于以下內(nèi)容進(jìn)行過濾:price、sqft、num_of_bedsnum_of_baths

如何在 JavaScript 中執(zhí)行類似下面的偽代碼的操作:

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

請注意,語法不必與上面完全相同。這只是一個例子。

P粉087951442
P粉087951442

全部回復(fù)(2)
P粉366946380

我很驚訝沒有人發(fā)布一行回復(fù):

const filteredHomes = json.homes.filter(x => x.price = 500 && x.num_of_beds >=2 && x.num_of_baths >= 2.5);

...只是為了讓您可以更輕松地閱讀:

const filteredHomes = json.homes.filter( x => 
  x.price = 500 && 
  x.num_of_beds >=2 && 
  x.num_of_baths >= 2.5
);
P粉315680565

您可以使用數(shù)組.prototype.filter方法:

var newArray = homes.filter(function (el) {
  return el.price = 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 = with a string and number
// will coerce the string to a number before comparing.)
var newArray = obj.homes.filter(function (el) {
  return el.price = 500 &&
         el.num_of_beds >= 2 &&
         el.num_of_baths >= 1.5; // Changed this so a home would match
});
console.log(newArray);
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板