how to use .filter javascript code example

Example 1: who to accses to an object vallue inside an array with .filter method js

const array = [
  {
    username: "john",
    team: "red",
    score: 5,
    items: ["ball", "book", "pen"]
  },
  {
    username: "becky",
    team: "blue",
    score: 10,
    items: ["tape", "backpack", "pen"]
  },
  {
    username: "susy",
    team: "red",
    score: 55,
    items: ["ball", "eraser", "pen"]
  },
  {
    username: "tyson",
    team: "green",
    score: 1,
    items: ["book", "pen"]
  },

];
//FILTER MEMBERS IN TEAM "RED"
const filterArray=array.filter(word=>word.team==="red")

Example 2: javascript array filter

var 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);