JavaScript array filter method

preview_player
Показать описание
Link for all dot net and sql server video tutorial playlists

Link for slides, code samples and text version of the video

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.

The filter() method creates a new array and populates that array with all the elements that meet the condition specified in a callback function.

Parameters
callbackFunction
Required. Function that gets called for each element of the array. If the function returns true, the element is kept otherwise filtered.

thisArg
Optional. An object to which the this keyword can refer in the callbackfn function.

The filter method calls the callbackfn function one time for each element in the array. If the callback function returns false for all elements of the array, the length of the new array that will be returned is 0.

Callback Function Syntax
function callbackFunction(value, index, array)

Callback Function Parameters
value
The value of the element in the array

index
The index position of the element in the array

array
The source array object that contains the element

Example 1 : Retrieve only even numbers from myArray

// Callback function
function IsEven(value, index, array)
{
if (value % 2 == 0)
{
return true;
}
else
{
return false;
}
}

// Source array
var myArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

// Pass the callback function as argument to the filter method

Output : 2,4,6,8,10

Example 2 : In Example 1 we defined a callback function first and then passed it as an argument to the filter() method. In the example below, we created the callback function as an anonymous function directly in the filter method where it is required.

// Source array
var myArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

// callback function created directly in the filter method as anonymous function

Output : 2,4,6,8,10

Example 3 : Remove duplicates from javascript array

var myArray = ["Sam", "Mark", "Tim", "Sam"];

Output :
Sam,Mark,Tim
Рекомендации по теме
Комментарии
Автор

I always come away from watching one of your videos feeling that I was actually TAUGHT something, rather than being SHOWN something... MANY THANKS for your efforts!

HawkIjr
Автор

I like that you did two examples for the filter method, and I love that you explained so thoroughly each line of code as you wrote it. Also, thanks for explaining as you went back to edit the code, removing the unnecessary statements. I feel like I understand the filter method much better after watching this video! Thank you for posting!

ChrissyAlbert
Автор

6:44 _"Let's look at another example."_ Thank you for covering the _index_ and _array_ parameters here. Most tutorials discuss only basic use with the _value_ parameter and don't bother going beyond this. Very helpful!

orangemancometh
Автор

Hey man, you're a great teacher. I like how you gave one basic example, and then another one using all of the callback function parameters. This channel will be my new secret weapon.

ralphedgar
Автор

The easiest way that I have seen to remove duplicates from a Javascript array. Thank you!

jayjg
Автор

I usually don't watch videos for programming solutions but you my friend has won over my heart! Thank you for this video!! Audio 10 Video 10 Explanation 140%. Absolutely amazing! Subscribing now!

johnmwangi
Автор

Thank you very much for breaking down the concept the way you have.

I've checked out 5 different filter videos, and yours is the only one that explains the Value, Index, and Array of the the CallBack function. The detail you provide in your tutorial makes the concept very digestible and clear...so, thanks for that.

sashaikevich
Автор

I've been trying to understand this all day from many sources, but this finally made sense to me. Thank you!

tomb
Автор

Dude, thank you ! After a few articles and tutorials (for the duplicates) i finally get it thanks to your clear explanation.

axeltavormina
Автор

Best video so far, straight to the point and no fucking around like everyone else.

boband
Автор

I like the way you break things down to the tiniest digestible bit. Keep it up.

ianyimiah
Автор

Thanks for making such content which is easy to digest. Just want to share code which I tried. In above video if you specify same string with different case like uppercase and lower case this won't work. Following code snippet can be helpful

var country = ["India", "USA", "Germany", "india"];
var convertLowerCase =

var removeDuplicate = (value, index, array) {

return == index;

});

console.log(removeDuplicate);

abhirocks
Автор

Thank you so much for your videos. I've only seen this one and the arguments video and you make the concepts easy to understand!!

mangonut
Автор

nice trick with indexOf for the filter function.

megawutt
Автор

This was a great tutorial, thank you for posting and keeping it basic with excellent use of example based teaching.

you_gotit_bud
Автор

thanks for this lessons and i hope try to Simplify the explanation but i understand you and you are a good instructor

Beta_Computer
Автор

Great explaination. Helped me to solve one problem I had with this method.

arturdomanski
Автор

You should do these same videos on Reduce, Map, forEach, and binding

candidatesvoice
Автор

thank you for making a concept crystal clear!

skisteepndeep
Автор

Thanks a lot for the great video. You go through it at a perfect pace. I will check out more videos!

jasonwelsh
join shbcf.ru