Recent Posts
- 10 Things To Do When Launching A Site
- Using PHP to connect to SFTP
- Asynchronous Virtual Pageviews with Google Analytics
- 5 Advanced Text Editing Keyboard Shortcuts
- Get the headers of a HTTP request with PHP
- How-to: Create PDF preview images in PHP – Part 2
- How-to: Create PDF preview images in PHP
- Quick Code: Get the domain name in JS
- Things to think about when designing a logo
- Javascript Array Functions
Topics
Javascript Array Functions
Posted on 29th March 2009 by Jacob Wyke
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.