I have the following JavaScript array of real estate family objects:
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;
What I want to do is be able to perform a filter on the objects to return a subset of the "home" objects.
For example, I would like to be able to filter based on: price
, sqft
, num_of_beds
, and num_of_baths
.
How to do something like the following pseudocode in JavaScript:
var newArray = homes.filter( price <= 1000 & sqft >= 500 & num_of_beds >=2 & num_of_baths >= 2.5 );
Please note that the syntax does not have to be exactly the same as above. This is just an example.
I'm surprised no one posted a one line reply:
...Just to make it easier for you to read:
You can use
array.prototype.filter
method:Example: