filter js w3schools code example

Example 1: 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 2: 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 3: filter javascript

function filter(array, filterfunc) {

  let filteredArray = [];
  for (let i = 0; i < array.length; i++) {
    let result = filterfunc(array[i], i, array);
    if (result) {
      filteredArray.push(array[i]);
    }
  }
  return filteredArray;
};