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
var numbers = [1, 3, 6, 8, 11];
var lucky = numbers.filter(function(number) {
return number > 7;
});
// [ 8, 11 ]
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 persons = [
{name:"Shirshak",gender:"male"},
{name:"Amelia",gender:"female"},
{name:"Amand",gender:"male"}
]
//filter return all objects in array
let male = persons.filter(person=>person.gender==='male')
console.log(male)
xxxxxxxxxx
const arr = ['pine', 'apple', 'pineapple', 'ball', 'roll'];
const longWords = arr.filter(item => item.length > 5);
console.log(longWords); // ['pineapple']
xxxxxxxxxx
let names = ['obi','bisi','ada','ego']
const res = names.filter(name => name.length > 3)
xxxxxxxxxx
function bouncer(arr) {
let newArray = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i]) newArray.push(arr[i]);
}
return newArray;
}
xxxxxxxxxx
//How To Use Array.filter() in JavaScript
//example 1.
const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const result = words.filter(word => word.length < 6);
console.log(result);
//OUTPUT: ['spray', 'limit', 'elite'
//example 2
const numbers = [45, 4, 9, 16, 25];
const over18 = numbers.filter(myFunction);
function myFunction(value, index, array) {
return value > 18;
}
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 numbers = [45, 4, 9, 16, 25];
const over18 = numbers.filter(myFunction);
function myFunction(value, index, array) {
return value > 18;
}
xxxxxxxxxx
let bigCities = cities.filter(city => city.population > 3000000);
console.log(bigCities);Code language: JavaScript (javascript)