JavaScript filter() method in 6 minutes! 🚰

preview_player
Показать описание
// .filter() = creates a new array by filtering out
// elements with a callback

00:00:00 example 1
00:02:13 example 2
00:04:01 example 3

// ----------- EXAMPLE 1 -----------
let numbers = [1, 2, 3, 4, 5, 6, 7];

function isEven(element){
return element % 2 === 0;
}

function isOdd(element){
return element % 2 !== 0;
}
Рекомендации по теме
Комментарии
Автор

// filter() = creates a new array by filtering out
// elements with a callback

// EXAMPLE 1
let numbers = [1, 2, 3, 4, 5, 6, 7];
let evenNums = numbers.filter(isEven);
let oddNums = numbers.filter(isOdd);

console.log(oddNums);

function isEven(element){
return element % 2 === 0;
}

function isOdd(element){
return element % 2 !== 0;
}

// EXAMPLE 2
let ages = [16, 17, 17, 18, 19, 20, 65];
let adults = ages.filter(isAdult);
let children = ages.filter(isChild);

console.log(children);

function isAdult(element){
return element >= 18;
}

function isChild(element){
return element < 18;
}

// EXAMPLE 3
const words = ['apple', 'orange', 'kiwi', 'banana', 'pomegranate', 'coconut', ];
const longWords = words.filter(getLongWords);
const shortWords = words.filter(getShortWords);

console.log(shortWords);

function getShortWords(element){
return element.length <= 6;
}

function getLongWords(element){
return element.length > 6;
}

BroCodez
Автор

I'm learning Python with your videos. The best material I could find so far. Thank you, bro.

joehaar
Автор

You made the code structure easier for me. Thank you so much brother❤

Aarif
Автор

One bad thing about JS is that it works when there is errors and it don't caught errors like python or ... and shows unexpected behavior due to these errors, I wonder if there is any linter for this kind of situation . but believe me it got 30 minutes to find that i had a typo in element.lenght and I did not see it . end everything work like there is nothing but with a bug

xzex
Автор

I use expressions element % 2 for odd or return !( element % 2), are these expressions are equal like == or strictly === 0 when we don't mention them ??

xzex
Автор

Please create small small project in js

sportsknowledge
Автор

One thing about JS that has been a bit confusing is as in your example about isEven function even thoufh it takes a parameter when you define it, when you use it in the .filter method you dont pass an argument in it. Why is that? I have just always taken it for granted but im genuinely curious why it works that way

nikolabosevski