what is the filter method code example
Example 1: how the filter() function works javascript
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const filter = arr.filter((number) => number > 5);
console.log(filter); // [6, 7, 8, 9]
Example 2: the filter array
var id = 2;
var list = [{
Id: 1,
Name: 'a'
}, {
Id: 2,
Name: 'b'
}, {
Id: 3,
Name: 'c'
}];
var lists = list.filter(x => {
return x.Id != id;
})
console.log(lists);
Example 3: javascript array filter
run.addEventListener("click", function () {
let array = [];
people.forEach((elem) => {
// elem before age to target
if (elem.age > 18) {
array.push(elem); // each array elem > 18 is "pushed" inside the new array
// console.log(array); nope : messes things up
} else {
(""); // no need to declare this through console.log
}
});
console.log(array); //and there you have it : filtered array
});
Example 4: js filter items by index
let newArray = arr.filter(callback(element[, index, [array]])[, thisArg])