JS filter


JS Array.filter() method returns the subset of elements which all satisty a specific function.

var arr = new Array(1,2,3,4,5);
var ret = arr.filter(function(x) {return x < 3;})
alert(ret); //1,2
ret = arr.filter(function(x) {return x % 2 == 0;})
alert(ret); //2,4

Pick up the nth elements of an array:

var arr = ["be", "32", "color", "9", "u", "2", "3.2", "i","we"];
var ret = arr.filter(
	function(value, index, arr) {
		return index % 2 == 0;
	});
alert(ret); //be,color,u,3.2,we
ret = arr.filter(
	function(value, index, arr) {
		return (index+1) % 3 == 0;
	})
alert(ret); //color,2,we