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}];
//find where title=B
let x = arr.filter((a)=>{if(a.title=='B'){return a}});
console.log(x)//[{id:3,title:'B',status:true}]
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”;
});
// [ {name: “Ironman”, franchise: “Marvel”}, {name: “Thor”, 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) {
// 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 4: javascript filter array of objects by array
var arr = [1,2,3,4],
brr = [2,4],
res = arr.filter(f => !brr.includes(f));
console.log(res);
Example 5: filter out arrays js
let newArray = array.filter(function(item) {
return condition;
});
Example 6: 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]