0

hello guys i have a doubt in es6 filter function

why is the callback function in filter function returning the value and not a boolean value. since i have returned value >=0; https://code.sololearn.com/cgJNZOGcbtfh/?ref=app

27th Sep 2021, 5:34 AM
CodeSmith
CodeSmith - avatar
8 ответов
+ 2
using single statement arrow function doesn't require return statement ( its optional only if {} is not used and there's only one statement who's value you want to return ) also your question is not clearly stated ! and yes its returning the result of a comparison which is boolean not a value .
27th Sep 2021, 5:52 AM
Prashanth Kumar
Prashanth Kumar - avatar
+ 2
all filter function does is returns the array for which the boolean value evaluates to true.. wait leme give you a simple implementation of it
27th Sep 2021, 5:55 AM
Prashanth Kumar
Prashanth Kumar - avatar
+ 2
This is how the filter function works under the hood Andy_Roid let arr = [1,2,-3,4,-5]; Array.prototype.my_filter=function(clbk){ // here we define our own prototype let new_arr=[]; for(let i=0;i<this.length;i++){ if(clbk(this[i])){ // evaluate to see if condition passes this is where your condition is evaluated (callback) new_arr.push(this[i]); } } return new_arr; } let wholeNumbers = arr.my_filter(value=>value>=0) console.log(wholeNumbers);
27th Sep 2021, 6:00 AM
Prashanth Kumar
Prashanth Kumar - avatar
+ 2
👍🏻 you got it right Andy_Roid
27th Sep 2021, 6:11 AM
Prashanth Kumar
Prashanth Kumar - avatar
+ 1
Prashanth Kumar thanks, i am confused since it is returning a boolean value shouldn't the wholeNumber variable be filled with boolean values?
27th Sep 2021, 5:54 AM
CodeSmith
CodeSmith - avatar
+ 1
Prashanth Kumar i have made some changes to the code(for better understanding)
27th Sep 2021, 5:58 AM
CodeSmith
CodeSmith - avatar
+ 1
Prashanth Kumar i think i get it now, so the callback function in actually returning boolean value and when it relates to true, than keep the value. thanks a lot bro.
27th Sep 2021, 6:10 AM
CodeSmith
CodeSmith - avatar
0
Hello, i start learn python..
28th Sep 2021, 7:45 AM
Malek AlAjil
Malek AlAjil - avatar