I was trying to code the function filter on es6 but i didn’t make it if someone could share me his knowledge i’ll be gratefull!
Where are you having problems?
var myArray = ["one", "two", "three"];
myArray.filter( (key) => key.length <= 3 );
Not in that way i was trying to code a function filter to understand his mechanism,
function filter(array,function){
let array2;
for (let v of array){
if (funcion(v)){array2.puts(v) ;}
}
return array2;
}
But using this i can’t use a function who use more than 1 arguments
Array.prototype.filter = function(callback){
var list = [];
for(var i=0; i<this.length; i++){
if(callback(this[i],i,this)) list.push(this[i]);
}
return list;
};
You can also use forEach method for the same result :
Array.prototype.filter = function(callback){
var list = [];
this.forEach(function(val,index,self){
if(callback(val,index,self)) list.push(val);
});
return list;
};
okay thanks mate !!!