Q:

array.filter in javascript

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

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

console.log(result);
26
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]
11
var numbers = [1, 3, 6, 8, 11];

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

// [ 8, 11 ]
2
const filtered = array.filter( item => item < 30)

// this will return an array with all the item that are less than 30
// ""item => item < 30"" this ia the function used for the filtering
3
var numbers = [1, 3, 6, 8, 11];

var lucky = numbers.filter(function(number) {
  return number > 7;
});
2
var newArray = array.filter(function(item) {
  return condition;
});
8
//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);
1
  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
  });
1

New to Communities?

Join the community