xxxxxxxxxx
const numbers = [5, 13, 7, 30, 5, 2, 19];
const bigNumbers = numbers.filter(number => number > 20);
console.log(bigNumbers);
//Output: [30]
xxxxxxxxxx
const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const result = words.filter(word => word.length < 6);
console.log(result);
//OUTPUT: ['spray', 'limit', 'elite']
xxxxxxxxxx
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" ]
xxxxxxxxxx
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]
xxxxxxxxxx
const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const threeLetterWords = words.filter(word => word.length <= 5)
console.log(threeLetterWords);
// RESULT: (3) ["spray", "limit", "elite"]
xxxxxxxxxx
const arr = [1, 2, 3, 4, 5, 6];
const filtered = arr.filter(el => el === 2 || el === 4); //[2, 4]
xxxxxxxxxx
let arr = [1,2,3,0,4,5]
let arr2=[0,2]
let ans = arr.filter(function(value,index,array){
return !this.includes(value)
},arr2)
console.log(ans)
xxxxxxxxxx
const grades = [10, 2, 21, 35, 50, -10, 0, 1];
// get all grades > 20
const result = grades.filter(grade => grade > 20); // [21, 35, 50];
// get all grades > 30
grades.filter(grade => grade > 30); // [35, 50]
xxxxxxxxxx
const arr = [1, 2, 3]
const result = arr.filter(number => number < 3)
// [1, 2]
console.log(result)
xxxxxxxxxx
const myNum = [2,3,4,5,6,7,8,9,10];
//using filter gives us a new array
const divisibleBy3 = myNum.filter(eachNum => eachNum%3==0);
console.log(divisibleBy3); //output:[3,6,9]