The filter() method in JavaScript is a powerful tool to create a new array containing only the elements that pass a specific test, defined by a provided function. It doesn't modify the original array, making it perfect for selecting specific items based on conditions.
In this blog, I'll explain the filter() method with a simple syntax breakdown and provide 50 straightforward examples using South Indian themes (like food, names, and cities). These examples avoid complex math and focus on easy-to-understand filtering tasks.
Here’s the basic syntax for filter():
const newArray = array.filter(function(element, index, array) { // Return true to keep the element, false to exclude it });
The filter() method returns a new array with only the elements that return true for the provided function.
const dishes = ['idli', 'dosa', 'vada']; const dDishes = dishes.filter(dish => dish.startsWith('d')); console.log(dDishes);
Output: ["dosa"]
const names = ['Raj', 'Priya', 'Surya']; const longNames = names.filter(name => name.length > 4); console.log(longNames);
Output: ["Priya", "Surya"]
const cities = ['Chennai', 'Bangalore', 'Madurai']; const aiCities = cities.filter(city => city.includes('ai')); console.log(aiCities);
Output: ["Chennai", "Madurai"]
const numbers = [5, 15, 20]; const bigNumbers = numbers.filter(num => num > 10); console.log(bigNumbers);
Output: [15, 20]
const foods = ['idli', 'dosa', 'vada']; const fourLetterFoods = foods.filter(food => food.length === 4); console.log(fourLetterFoods);
Output: ["idli", "dosa", "vada"]
const nums = [1, 2, 3, 4]; const evens = nums.filter(num => num % 2 === 0); console.log(evens);
Output: [2, 4]
const names = ['Surya', 'Arjun', 'Sita']; const sNames = names.filter(name => name.startsWith('S')); console.log(sNames);
Output: ["Surya", "Sita"]
const ages = [16, 18, 20]; const adults = ages.filter(age => age >= 18); console.log(adults);
Output: [18, 20]
const langs = ['Tamil', 'Telugu', 'Kannada']; const uLangs = langs.filter(lang => lang.endsWith('u')); console.log(uLangs);
Output: ["Telugu"]
const nums = [-1, 0, 5, 10]; const positives = nums.filter(num => num > 0); console.log(positives);
Output: [5, 10]
const dishes = ['sambar', 'rasam', 'idli']; const aDishes = dishes.filter(dish => dish.includes('a')); console.log(aDishes);
Output: ["sambar", "rasam"]
const cities = ['Chennai', 'Kochi', 'Bangalore']; const longCities = cities.filter(city => city.length > 6); console.log(longCities);
Output: ["Bangalore"]
const bools = [true, false, true]; const trues = bools.filter(b => b === true); console.log(trues);
Output: [true, true]
const festivals = ['Pongal', 'Onam', 'Diwali']; const pFestivals = festivals.filter(fest => fest.startsWith('P')); console.log(pFestivals);
Output: ["Pongal"]
const scores = [45, 60, 30]; const lowScores = scores.filter(score => score < 50); console.log(lowScores);
Output: [45, 30]
const names = ['Anita', 'Vijay', 'Lakshmi']; const aNames = names.filter(name => name.endsWith('a')); console.log(aNames);
Output: ["Anita", "Lakshmi"]
const strings = ['idli', '', 'dosa']; const nonEmpty = strings.filter(str => str.length > 0); console.log(nonEmpty);
Output: ["idli", "dosa"]
const people = [{name: 'Raj', age: 30}, {name: 'Meena', age: 22}]; const older = people.filter(p => p.age > 25); console.log(older);
Output: [{name: "Raj", age: 30}]
const cities = ['Chennai', 'Madurai', 'Coimbatore']; const cCities = cities.filter(city => city.startsWith('C')); console.log(cCities);
Output: ["Chennai", "Coimbatore"]
const nums = [1, 2, 3, 4]; const odds = nums.filter(num => num % 2 !== 0); console.log(odds);
Output: [1, 3]
const dishes = ['idli', 'sambar', 'rasam']; const longDishes = dishes.filter(dish => dish.length > 4); console.log(longDishes);
Output: ["sambar", "rasam"]
const names = ['Arjun', 'Priya', 'Surya']; const iNames = names.filter(name => name.includes('i')); console.log(iNames);
Output: ["Arjun", "Priya"]
const nums = [0, 1, 0, 2]; const zeros = nums.filter(num => num === 0); console.log(zeros);
Output: [0, 0]
const states = ['Kerala', 'Tamilnadu', 'Karnataka']; const kStates = states.filter(state => state.startsWith('K')); console.log(kStates);
Output: ["Kerala", "Karnataka"]
const scores = [70, 80, 90]; const highScores = scores.filter(score => score > 75); console.log(highScores);
Output: [80, 90]
const snacks = ['murukku', 'vada', 'bonda']; const uSnacks = snacks.filter(snack => snack.endsWith('u')); console.log(uSnacks);
Output: ["murukku"]
const items = ['rice', 'dal', 'curry', 'idli']; const evenIndexed = items.filter((item, i) => i % 2 === 0); console.log(evenIndexed);
Output: ["rice", "curry"]
const people = [{name: 'Anu', age: 22}, {name: 'Vikram', age: 28}]; const aPeople = people.filter(p => p.name.startsWith('A')); console.log(aPeople);
Output: [{name: "Anu", age: 22}]
const places = ['Madurai', 'Ooty', 'Mysore']; const mPlaces = places.filter(place => place.includes('m')); console.log(mPlaces);
Output: ["Madurai", "Mysore"]
const nums = [-5, 10, -2, 15]; const negatives = nums.filter(num => num < 0); console.log(negatives);
Output: [-5, -2]
const sweets = ['laddu', 'jalebi', 'mysorepak']; const lSweets = sweets.filter(sweet => sweet.startsWith('l')); console.log(lSweets);
Output: ["laddu"]
const words = ['idli', 'sambar', 'vada']; const shortWords = words.filter(word => word.length < 5); console.log(shortWords);
Output: ["idli", "vada"]
const temples = ['Madurai', 'Tirupati', 'Chennai']; const iTemples = temples.filter(temple => temple.endsWith('i')); console.log(iTemples);
Output: ["Madurai", "Tirupati"]
const nums = [3, 4, 6, 7]; const divBy3 = nums.filter(num => num % 3 === 0); console.log(divBy3);
Output: [3, 6]
const dances = ['Bharatanatyam', 'Kuchipudi', 'Odissi']; const aDances = dances.filter(dance => dance.includes('a')); console.log(aDances);
Output: ["Bharatanatyam"]
const ages = [25, 35, 28]; const young = ages.filter(age => age < 30); console.log(young);
Output: [25, 28]
const words = ['Tamil', 'Nadu', 'Telugu']; const tWords = words.filter(word => word.startsWith('T')); console.log(tWords);
Output: ["Tamil", "Telugu"]
const people = [{name: 'Karthik', age: 20}, {name: 'Sowmya', age: 22}]; const age20 = people.filter(p => p.age === 20); console.log(age20);
Output: [{name: "Karthik", age: 20}]
const fruits = ['mango', 'banana', 'coconut']; const aFruits = fruits.filter(fruit => fruit.includes('a')); console.log(aFruits);
Output: ["mango", "banana"]
const nums = [5, 10, 15, 5]; const notFive = nums.filter(num => num !== 5); console.log(notFive);
Output: [10, 15]
const curries = ['sambar', 'rasam', 'kootu']; const sCurries = curries.filter(curry => curry.startsWith('s')); console.log(sCurries);
Output: ["sambar"]
const words = ['sambar', 'idli', 'rasam']; const fiveLetter = words.filter(word => word.length === 5); console.log(fiveLetter);
Output: ["sambar", "rasam"]
const items = ['rice', 'dal', 'curry', 'idli']; const oddIndexed = items.filter((item, i) => i % 2 !== 0); console.log(oddIndexed);
Output: ["dal", "idli"]
const names = ['Ram', 'Sita', 'Vikram']; const mNames = names.filter(name => name.endsWith('m')); console.log(mNames);
Output: ["Ram", "Vikram"]
const scores = [90, 100, 80]; const below100 = scores.filter(score => score < 100); console.log(below100);
Output: [90, 80]
const places = ['Ooty', 'Mysore', 'Chennai']; const ePlaces = places.filter(place => place.endsWith('e')); console.log(ePlaces);
Output: ["Mysore"]
const sweets = ['laddu', 'jalebi', 'mysorepak']; const iSweets = sweets.filter(sweet => sweet.includes('i')); console.log(iSweets);
Output: ["jalebi"]
const ages = [25, 30, 20]; const not25 = ages.filter(age => age !== 25); console.log(not25);
Output: [30, 20]
const dances = ['Bharatanatyam', 'Kuchipudi', 'Odissi']; const longDances = dances.filter(dance => dance.length > 7); console.log(longDances);
Output: ["Bharatanatyam", "Kuchipudi"]
const items = ['rice', 'dal', 'curry']; const firstTwo = items.filter((item, i) => i < 2); console.log(firstTwo);
Output: ["rice", "dal"]
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions