Accessibility Navigation

Javascript Array Functions

Posted on 29th March 2009 by Jacob

Without using a Javascript library such as JQuery, there are very few functions built into Javascript to deal with arrays. Here are a few of my own that often come in handy when working on projects.

In Array

This function checks to see if a value is in an array. JQuery has a built in function of the same name that does the same thing.

function inArray(value, array){
	for(var x=0;x<array.length;x++){
		if(array[x]==value){
			return 1;
		}
	}

	return 0;
}

Remove From Array

This function removes a value from an array.

function removeFromArray(value, array){
	for(var x=0;x<array.length;x++){
		if(array[x]==value){
			array.splice(x, 1);
		}
	}

	return array;
}

Simple bits of code, but of a lot of use when creating dynamic client side interfaces with Javascript. Let me know in the comments if you have any other little functions that you often use.


Have Your Say

Have Your Say Form









Comments

  • Be the first to add your voice to this post!

    Start the discussion and leave your views or thoughts by writing a comment using the form above.