Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (9.5k points)

Below is the JavaScript array of real estate home 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;

I want to filter on the object based on price, sqft, num_of_beds, and num_of_baths to return a subset of "home" objects. Below is the pseudo-code for it:


 

var newArray = homes.filter(

    price <= 1000 & 

    sqft >= 500 & 

    num_of_beds >=2 & 

    num_of_baths >= 2.5 );

Can anyone tell me how to do this?

1 Answer

0 votes
by (19.7k points)

Try to use the Array.prototype.filter method:

var newArray = homes.filter(function (el) {

  return el.price <= 1000 &&

         el.sqft >= 500 &&

         el.num_of_beds >=2 &&

         el.num_of_baths >= 2.5;

});


 

Interested in Java? Check out this Java tutorial by Intellipaat.   

Browse Categories

...