javascript filter array of objects and return value code example
Example 1: how to filter an array of objects in javascript
let arr=[{id:1,title:'A', status:true}, {id:3,title:'B',status:true}, {id:2, title:'xys', status:true}];
let x = arr.filter((a)=>{if(a.title=='B'){return a}});
console.log(x)
Example 2: js filter array of objects by value
var heroes = [
{name: “Batman”, franchise: “DC”},
{name: “Ironman”, franchise: “Marvel”},
{name: “Thor”, franchise: “Marvel”},
{name: “Superman”, franchise: “DC”}
];
var marvelHeroes = heroes.filter(function(hero) {
return hero.franchise == “Marvel”;
});
Example 3: 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) {
return currentElement.country === "America" && currentElement.age < 25;
});
console.log(filteredPeople);