Jojoxx.net logotyp

 

 

Användare ikonAnvändare
Besökare just nu: 54
Snabblänkar ikonSnabblänkar
JavaScript-arkivet
Perl
FAQ

Sök på Jojoxx.net


Array prototypes

Kod


Array.prototype.shuffle=function(){
	// Usage: newArray=myArray.shuffle();
	// Randomizes the order of an array and returns it.
	return this.sort(function(){ return 0.5-Math.random() } ) 
}
Array.prototype.deleteWithValue=function(value){
	// Usage: newArray=myArray.deleteWithValue(value);
	// Removes elements from an Array based on value and returns the array. The value can be either a string or a RegExp.
	for(var i=this.length-1;i>=0;i--){ if(typeof(value)=="string" && this[i]==value || typeof(value)=="object" && value.test(this[i])){ this.splice(i,1); } }
	return this;
}
Array.prototype.qw=function(string){
	// Usage: newArray=new Array().qw("foo bar");
	// Returns an array with "words" separated from the given string.
	return string.split(/\s+/);
}
Array.prototype.contains=function(value){
	// Usage: myBoolean=myArray.contains("value");
	// Return true if the array matches the value given. The value can be either a string or a RegExp.
	for(var i=this.length-1;i>=0;i--){
		if(typeof(value)=="string" && this[i]==value || typeof(value)=="object" && value.test(this[i])){ return true; }
	}
}
Array.prototype.match=function(value){
	// Usage: newArray=myArray.match("value");
	// Returns a new array with values that match the current array. The value can be either a string or a RegExp.
	var a=new Array();
	for(var i=this.length-1;i>=0;i--){
		if(typeof(value)=="string" && this[i]==value || typeof(value)=="object" && value.test(this[i])){ a.push(this[i]); }
	}
	return a;
}
Array.prototype.exclude=function(value){
	// Usage: newArray=myArray.exclude("value");
	// Returns a new array with values that does not match the current array. The value can be either a string or a RegExp.
	var a=new Array();
	for(var i=this.length-1;i>=0;i--){
		if(!(typeof(value)=="string" && this[i]==value || typeof(value)=="object" && value.test(this[i]))){ a.push(this[i]); }
	}
	return a;
}

Kommentar

Lite allmänna array prototypes som kan vara bra att ha.


,
Denna webbplats använder cookies. Genom att använda denna webbplats accepterar du användandet av cookies.