filters in javascript 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: javascript filter

const filtered = array.filter(item => {
    return item < 20;
});
// An example that will loop through an array
// and create a new array containing only items that
// are less than 20. If array is [13, 65, 101, 19],
// the returned array in filtered will be [13, 19]

Example 3: 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 4: javascript array filter

var numbers = [1, 3, 6, 8, 11];

var lucky = numbers.filter(function(number) {
  return number > 7;
});

// [ 8, 11 ]

Example 5: javascript array filter

var newArray = array.filter(function(item) {
  return condition;
});

Example 6: 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
  });