Recent Posts
- Quick Code: Get the domain name in JS
- Things to think about when designing a logo
- Javascript Array Functions
- Integrating FCKEditor to save current content with AJAX using PHP
- Pad A Javascript Number
- Why use heading tags as opposed to font tags for displaying text in HTML?
- Creating One Time Download Links
- Why Javascript in Forms is Bad
- Five Things To Remember When Designing Accessible Web Pages
- Sizing Images Correctly – Part Two
Topics
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.
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.