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
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 .
+ 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
+ 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);
+ 2
👍🏻 you got it right Andy_Roid
+ 1
Prashanth Kumar thanks, i am confused since it is returning a boolean value shouldn't the wholeNumber variable be filled with boolean values?
+ 1
Prashanth Kumar i have made some changes to the code(for better understanding)
+ 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.
0
Hello, i start learn python..