# Function filter

**URL:** <https://discourse.mozilla.org/t/function-filter/20901>\
**Category:** Development\
**Created:** [October 26, 2017, 9:08pm UTC](https://discourse.mozilla.org/t/function-filter/20901 "2017-10-26T21:08:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicorabarisoa1](https://avatars.discourse-cdn.com/v4/letter/n/9f8e36/32.png) [@nicorabarisoa1](https://discourse.mozilla.org/u/nicorabarisoa1)\
**Post date:** [October 26, 2017, 9:08pm UTC](https://discourse.mozilla.org/t/function-filter/20901/1 "2017-10-26T21:08:50Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![kip](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/kip/32/15539_2.png) [@kip](https://discourse.mozilla.org/u/kip)\
**Post date:** [October 26, 2017, 10:45pm UTC](https://discourse.mozilla.org/t/function-filter/20901/2 "2017-10-26T22:45:24Z")

</div>

Where are you having problems?

```auto
var myArray = ["one", "two", "three"];

myArray.filter( (key) => key.length <= 3 );

```

---

<div class="post-metadata">

**Author:** ![nicorabarisoa1](https://avatars.discourse-cdn.com/v4/letter/n/9f8e36/32.png) [@nicorabarisoa1](https://discourse.mozilla.org/u/nicorabarisoa1)\
**Post date:** [October 27, 2017, 10:34am UTC](https://discourse.mozilla.org/t/function-filter/20901/3 "2017-10-27T10:34:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Baptistou](https://avatars.discourse-cdn.com/v4/letter/b/94ad74/32.png) [@Baptistou](https://discourse.mozilla.org/u/Baptistou)\
**Post date:** [October 27, 2017, 10:39am UTC](https://discourse.mozilla.org/t/function-filter/20901/4 "2017-10-27T10:39:44Z")

</div>

```

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;
};

```

---

<div class="post-metadata">

**Author:** ![Baptistou](https://avatars.discourse-cdn.com/v4/letter/b/94ad74/32.png) [@Baptistou](https://discourse.mozilla.org/u/Baptistou)\
**Post date:** [October 27, 2017, 10:47am UTC](https://discourse.mozilla.org/t/function-filter/20901/5 "2017-10-27T10:47:26Z")

</div>

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;
};

```

---

<div class="post-metadata">

**Author:** ![nicorabarisoa1](https://avatars.discourse-cdn.com/v4/letter/n/9f8e36/32.png) [@nicorabarisoa1](https://discourse.mozilla.org/u/nicorabarisoa1)\
**Post date:** [October 27, 2017, 11:00am UTC](https://discourse.mozilla.org/t/function-filter/20901/6 "2017-10-27T11:00:36Z")

</div>

okay thanks mate !!!
