Removing an element from an array specifying a value in Javascript
arrays, javascript
Solution
You want to use the splice() function to remove the item, indexOf will find it in the array:
To Find a specific element in the Array: (to know which to remove)
var index = array.indexOf('test2');
Full Example:
var array = ['test1', 'test2', 'test3'];
var value_to_remove = 'test2';
array.splice(array.indexOf(value_to_remove), 1);
Working Demo
Problem
I have read this question: Deleting array elements in JavaScript - delete vs splice And it appears that both splice and delete require an index of the element in order to remove, so how can I easily find the index when I have the value? For example if I have an array that looks like this: ``` ["test1", "test2", "test3"] ``` and I want to remove test2. The process I am using right now, which I'm hoping isn't the correct way to do it, is using `$.each` checking the value of each element in the array, maintaining a counter through the process (used as the index reference) and if the value is equal to "test2", then I have my index (in form of the counter) and then use splice to remove it. While the array grows larger, I would imagine this would be a slow process, but what alternatives do I have?