Home > Web Front-end > JS Tutorial > How to Filter a JavaScript Array of Home Objects Based on Attributes?

How to Filter a JavaScript Array of Home Objects Based on Attributes?

Barbara Streisand
Release: 2024-12-20 13:24:09
Original
480 people have browsed it

How to Filter a JavaScript Array of Home Objects Based on Attributes?

Filtering Object Arrays Based on Attributes in JavaScript

Problem:

You have an array of real estate home objects. You need to filter the array to return a subset of "home" objects based on specific criteria, such as price, square footage, number of bedrooms, and number of bathrooms.

Solution:

To filter an object array based on attributes, you can use the Array.prototype.filter method. This method takes a callback function as an argument, which is used to determine whether each element in the array should be included in the filtered array.

In your case, you can create a callback function that checks if each home object meets all of your criteria. Here's an example:

let newArray = homes.filter(function (el) {
  return el.price <= 1000 &&
         el.sqft >= 500 &&
         el.num_of_beds >= 2 &&
         el.num_of_baths >= 2.5;
});
Copy after login

This callback function checks if the price attribute is less than or equal to 1000, if the sqft attribute is greater than or equal to 500, if the num_of_beds attribute is greater than or equal to 2, and if the num_of_baths attribute is greater than or equal to 2.5. If all of these conditions are met, the element is included in the filtered array.

Note that the syntax does not have to be exactly like the example you provided. The above example uses the && operator to combine the conditions, but you could also use the & operator. Additionally, you could use arrow functions to simplify the callback function.

Here's a live example:

<script>
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) ...     
    ]
};
var newArray = obj.homes.filter(function (el) {
  return el.price <= 1000 &&
         el.sqft >= 500 &&
         el.num_of_beds >= 2 &&
         el.num_of_baths >= 2.5;
});
console.log(newArray);
</script>
Copy after login

The above is the detailed content of How to Filter a JavaScript Array of Home Objects Based on Attributes?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template