javascript filter array specify return object code example
Example 1: javascript filter array of objects
let people = [
{ name: "Steve", age: 27, country: "America" },
{ name: "Jacob", age: 24, country: "America" }
];
let filteredPeople = people.filter(function (currentElement) {
// the current value is an object, so you can check on its properties
return currentElement.country === "America" && currentElement.age < 25;
});
console.log(filteredPeople);
// [{ name: "Jacob", age: 24, country: "America" }]
Example 2: javascript filter array of objects
function isBigEnough(value) {
return value >= 10
}
let filtered = [12, 5, 8, 130, 44].filter(isBigEnough)
// filtered is [12, 130, 44]