filter array by object property javascript code example
Example 1: 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 2: filter out object in array using two arguments
var arr= [{id: "123", name: "Faa"},
{id: "123", name: "Bar"},
{id: "345", name: "Foo"},
{id: "678", name: "FaaBar"}
];
var name = 'FaaBar';
var id = '678';
arr = arr.filter(function(elem) {
return !(elem.id == id && elem.name == name)
});
Example 3: javascript array filter elements greater than
function isGreater(element, index, array) {
return (element >= 10);
}
let testElemets = [12, 5, 8, 130, 44].filter(isGreater);
console.log("Test Value : " + testElemets );
Example 4: filter out arrays js
let newArray = array.filter(function(item) {
return condition;
});
Example 5: javascript filter array of objects
function isBigEnough(value) {
return value >= 10
}
let filtered = [12, 5, 8, 130, 44].filter(isBigEnough)