how to use filter or search in array using javascript code example
Example 1: filter in js
const filterThisArray = ["a","b","c","d","e"]
console.log(filterThisArray) // Array(5) [ "a","b","c","d","e" ]
const filteredThatArray = filterThisArray.filter((item) => item!=="e")
console.log(filteredThatArray) // Array(4) [ "a","b","c","d" ]
Example 2: 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
});