javsacript 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 out arrays js

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

Example 3: array.filter in javascript

//Eg 1 - If item is unchecked, remove it from array

const array = [2, 3, 4]

if (event.target.checked) {
      newValue.push(option);
    } else {
      newValue = newValue.filter(item => item.id != option.id);
    }

//If false, it will remove the element. 2 is not equal to 2.

//Eg 2 - To return an array with length > 6

const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const result = words.filter(word => word.length > 6);

Example 4: filter an array in Javascript

1let greaterTen = [];2
3for (let i = 0; i<numbers.length; i++) {4  var currentNumber = numbers[i];5  if (currentNumber > 10) {6    greaterTen.push(currentNumber)7  }8}9
10console.log(greaterTen); // [23, 12, 45, 78, 11, 10.1, 84]

Tags:

Php Example