javascript array filter callback code example
Example 1: who to accses to an object vallue inside an array with .filter method js
const array = [
{
username: "john",
team: "red",
score: 5,
items: ["ball", "book", "pen"]
},
{
username: "becky",
team: "blue",
score: 10,
items: ["tape", "backpack", "pen"]
},
{
username: "susy",
team: "red",
score: 55,
items: ["ball", "eraser", "pen"]
},
{
username: "tyson",
team: "green",
score: 1,
items: ["book", "pen"]
},
];
//FILTER MEMBERS IN TEAM "RED"
const filterArray=array.filter(word=>word.team==="red")
Example 2: 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 3: js filter items by index
let newArray = arr.filter(callback(element[, index, [array]])[, thisArg])