javascript how to use filter code example

Example 1: filter javascript array

var words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

const result = words.filter(word => word.length > 6);

console.log(result);

Example 2: filter 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]

or 

const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

const result = words.filter(word => word.length > 6);

console.log(result);
// expected output: Array ["exuberant", "destruction", "present"]

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: how to create my own filter in js

// filter takes an array and function as argumentfunction 
filter(arr, filterFunc) {
  const filterArr = []; // empty array        
  // loop though array    
  for(let i=0;i<arr.length;i++) {        
    const result = filterFunc(arr[i], i, arr);        
    // push the current element if result is true        
    if(result)             
      filterArr.push(arr[i]);     
  }    
  return filterArr;
}