xxxxxxxxxx
//filter numbers divisible by 2 or any other digit using modulo operator; %
const figures = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const divisibleByTwo = figures.filter((num) => {
return num % 2 === 0;
});
console.log(divisibleByTwo);
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
var numbers = [1, 3, 6, 8, 11];
var lucky = numbers.filter(function(number) {
return number > 7;
});
// [ 8, 11 ]
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 arr = [1, 2, 3, 4, 5, 6];
const filtered = arr.filter(el => el === 2 || el === 4); //[2, 4]
xxxxxxxxxx
const numbers = [5, 13, 7, 30, 5, 2, 19];
const bigNumbers = numbers.filter(number => number > 20);
console.log(bigNumbers);
//Output: [30]
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 videos = ['html', 'css', 'javascript'];
//filter return multiple values in array
const shortSearch = videos.filter(function (video) {
return video.length <= 5;
});
console.log(shortSearch);
const games = [
{ title: 'Mass Effect', rating: 9.5 },
{ title: 'League of Legends', rating: 5 },
{ title: 'Last of Us', rating: 10 },
{ title: 'God of War', rating: 8.5 },
{ title: 'WWE 2k20', rating: 6 },
];
const goodGames = games.filter(function (game) {
return game.rating > 8;
});
console.log(goodGames);
xxxxxxxxxx
let arr = [1, 2, 3, 4, 5]; // filter function in JS
let newArr = arr.filter((item) => {
return item % 2 == 0;
})
console.log(newArr)
// -- filters the array and return those items which are even