Javascript的:如何过滤基于属性的对象数组?

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

我想要做的就是能够在对象上执行filter来返回一部分“主”对象。

例如,我希望能够根据: pricesqftnum_of_bedsnum_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); 

你可以尝试使用像jLinq这样的框架 – 以下是使用jLinq的代码示例

 var results = jLinq.from(data.users) .startsWith("first", "a") .orEndsWith("y") .orderBy("admin", "age") .select(); 

欲了解更多信息,你可以按照链接http://www.hugoware.net/projects/jlinq

我更喜欢Underscore框架。 它提出了许多有用的对象操作。 你的任务:

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

希望它对你有用!

这里是使用jquery MAP函数在IE8中正常工作的小提琴

http://jsfiddle.net/533135/Cj4j7/

 json.HOMES = $.map(json.HOMES, function(val, key) { if (Number(val.price) <= 1000 && Number(val.sqft) >= 500 && Number(val.num_of_beds) >=2 && Number(val.num_of_baths ) >= 2.5) return val; }); 

你可以使用jQuery.grep(),因为jQuery 1.0:

 $.grep(homes, function (h) { return h.price <= 1000 && h.sqft >= 500 && h.num_of_beds >= 2 && h.num_of_baths >= 2.5 }); 

你可以很容易地做到这一点 – 可能有很多的实现可以select,但这是我的基本想法(可能有一些格式,你可以使用jQuery迭代对象,我现在不能想起它):

 function filter(collection, predicate) { var result = new Array(); var length = collection.length; for(var j = 0; j < length; j++) { if(predicate(collection[j]) == true) { result.push(collection[j]); } } return result; } 

然后你可以像这样调用这个函数:

 filter(json, function(element) { if(element.price <= 1000 && element.sqft >= 500 && element.num_of_beds > 2 && element.num_of_baths > 2.5) return true; return false; }); 

这样,您可以根据您定义的谓词调用筛选器,甚至可以使用较小的筛选器多次筛选。

您可以自己实现一个满足您的需求的过滤方法,具体方法如下:

 function myfilter(array, test){ var passedTest =[]; for (var i = 0; i < array.length; i++) { if(test( array[i])) passedTest.push(array[i]); } return passedTest; } var passedHomes = myfilter(homes,function(currentHome){ return ((currentHome.price <= 1000 )&& (currentHome.sqft >= 500 )&&(currentHome.num_of_beds >=2 )&&(currentHome.num_of_baths >= 2.5)); }); 

希望,它有帮助!

或者你可以简单地使用$.each (这也适用于对象,不仅仅是数组),并像这样构build一个新的数组:

 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) ... { "home_id": "3-will-be-matched", "price": "925", "sqft": "1000", "num_of_beds": "2", "num_of_baths": "2.5", }, ] } var homes = []; $.each(json.homes, function(){ if (this.price <= 1000 && this.sqft >= 500 && this.num_of_beds >= 2 && this.num_of_baths >= 2.5 ) { homes.push(this); } });