Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
92
rated 0 times [  98] [ 6]  / answers: 1 / hits: 109748  / 13 Years ago, mon, september 5, 2011, 12:00:00

I have an array like this



arr = [orange,red,black,white]


I want to augment the array object defining a deleteElem() method which acts like this:



arr2 = arr.deleteElem(red); // [orange,black,white] (with no hole)


What is the best way to accomplish this task using just the value parameter (no index)?


More From » arrays

 Answers
7

Here's how it's done:



var arr = [orange,red,black,white];
var index = arr.indexOf(red);
if (index >= 0) {
arr.splice( index, 1 );
}


This code will remove 1 occurency of red in your Array.


[#90256] Saturday, September 3, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mckinleyk

Total Points: 730
Total Questions: 99
Total Answers: 99

Location: South Georgia
Member since Fri, Nov 13, 2020
4 Years ago
;