hAnDoFhOnOr Posted February 7, 2006 Share Posted February 7, 2006 i know that you can use the splice() function to remove elements in an array by specifying the index of the element you want to remove. my question is what if you do not know the index but know the specific element you want to remove.e.g. array animal = new Array ("cat, dog, mouse, rat, cow");if you want to remove the element mouse, you can do animal.splice(2,0) right?but what if i don't know the index, is there a javascript function that lets you search for a specific element index. Does array.indexOf(array_element) work in javascript? thanks. Link to comment Share on other sites More sharing options...
Chocolate570 Posted February 7, 2006 Share Posted February 7, 2006 Try this page.http://www.w3schools.com/jsref/jsref_splice.asp Link to comment Share on other sites More sharing options...
hAnDoFhOnOr Posted February 7, 2006 Author Share Posted February 7, 2006 i have seen that page already, as i've said i know i can use the splice() function but i need something that can determine the index of a specific element in an array. I have been trying to usevar arr_index = array.indexOf(array_element); so that i can use array.splice(arr_index, 0); to remove the element. But it doesn't seem to work, any thoughts? I'll appreciate it very much. Link to comment Share on other sites More sharing options...
aspnetguy Posted February 7, 2006 Share Posted February 7, 2006 var animal = new Array ("cat, dog, mouse, rat, cow");int i;for(i=0;i<animal.length;i++){ if(animal[i] == "mouse") { animal.splice(i,0); }} try that Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now