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

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

我有以下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;

我想要做的是能够对对象执行过滤器以返回“home”对象的子集。

例如,我想基于对能够过滤:pricesqftnum_of_beds,和num_of_baths

如何在JavaScript中执行某些操作,如下面的伪代码:

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

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



吃鸡游戏
浏览 1212回答 3
3回答

人到中年有点甜

您可以使用以下Array.prototype.filter方法:var&nbsp;newArray&nbsp;=&nbsp;homes.filter(function&nbsp;(el)&nbsp;{ &nbsp;&nbsp;return&nbsp;el.price&nbsp;<=&nbsp;1000&nbsp;&& &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;el.sqft&nbsp;>=&nbsp;500&nbsp;&& &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;el.num_of_beds&nbsp;>=2&nbsp;&& &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;el.num_of_baths&nbsp;>=&nbsp;2.5;});实例:var obj = {&nbsp; &nbsp; 'homes': [{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "home_id": "1",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "price": "925",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "sqft": "1100",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "num_of_beds": "2",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "num_of_baths": "2.0",&nbsp; &nbsp; &nbsp; &nbsp; }, {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "home_id": "2",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "price": "1425",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "sqft": "1900",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "num_of_beds": "4",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "num_of_baths": "2.5",&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; // ... (more homes) ...&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; ]};// (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) {&nbsp; return el.price <= 1000 &&&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;el.sqft >= 500 &&&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;el.num_of_beds >= 2 &&&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;el.num_of_baths >= 1.5; // Changed this so a home would match});console.log(newArray);此方法是新ECMAScript第5版标准的一部分,几乎可以在所有现代浏览器中找到。对于IE,您可以包含以下方法以实现兼容性:if&nbsp;(!Array.prototype.filter)&nbsp;{ &nbsp;&nbsp;Array.prototype.filter&nbsp;=&nbsp;function(fun&nbsp;/*,&nbsp;thisp*/)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;len&nbsp;=&nbsp;this.length&nbsp;>>>&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(typeof&nbsp;fun&nbsp;!=&nbsp;"function") &nbsp;&nbsp;&nbsp;&nbsp;throw&nbsp;new&nbsp;TypeError(); &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;res&nbsp;=&nbsp;[]; &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;thisp&nbsp;=&nbsp;arguments[1]; &nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(var&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;<&nbsp;len;&nbsp;i++)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(i&nbsp;in&nbsp;this)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;val&nbsp;=&nbsp;this[i]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(fun.call(thisp,&nbsp;val,&nbsp;i,&nbsp;this)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;res.push(val); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;res; &nbsp;&nbsp;};}

慕丝7291255

我更喜欢Underscore框架。它建议对象有许多有用的操作。你的任务:var&nbsp;newArray&nbsp;=&nbsp;homes.filter( &nbsp;&nbsp;&nbsp;&nbsp;price&nbsp;<=&nbsp;1000&nbsp;&&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;sqft&nbsp;>=&nbsp;500&nbsp;& &nbsp;&nbsp;&nbsp;&nbsp;num_of_beds&nbsp;>=2&nbsp;&&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;num_of_baths&nbsp;>=&nbsp;2.5);可以覆盖像:var&nbsp;newArray&nbsp;=&nbsp;_.filter&nbsp;(homes,&nbsp;function(home)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;home.price<=1000&nbsp;&&&nbsp;sqft>=500&nbsp;&&&nbsp;num_of_beds>=2&nbsp;&&&nbsp;num_of_baths>=2.5;});希望它对你有用!
打开App,查看更多内容
随时随地看视频慕课网APP