6

I have an array

var array = ["google","chrome","os","windows","os"];

I want to delete the value "chrome" from the array without the array becoming a string. Is there a way to do this?

Marcel Korpel
21.9k6 gold badges63 silver badges80 bronze badges
asked Sep 22, 2010 at 22:31
2
  • 18
    I find it hard to believe that someone named Chromedude would want to delete Chrome from his array. Commented Sep 22, 2010 at 22:36
  • @ChessWhiz haha, did not even think of that, but it is in my array and it does need to be deleted :) Commented Sep 22, 2010 at 22:47

6 Answers 6

5

There's no faster way than finding it and then removing it. Finding it you can do with a loop or (in implementations that support it) indexOf. Removing it you can do with splice.

Live example: http://jsbin.com/anuta3/2

var array, index;
array = ["google","chrome","os","windows","os"];
if (array.indexOf) {
 index = array.indexOf("chrome");
}
else {
 for (index = array.length - 1; index >= 0; --index) {
 if (array[index] === "chrome") {
 break;
 }
 }
}
if (index >= 0) {
 array.splice(index, 1);
}
answered Sep 22, 2010 at 22:36
Sign up to request clarification or add additional context in comments.

5 Comments

Would that work in IE? I seem to recall seeing somewhere that it doesn't/didn't support indexOf?
Like TJ mentioned, not all browsers have indexOf implemented. You might need to implement your own.
@David: Not all implementations have it, hence the "with a loop" part of the answer. :-) Usually I just add indexOf to Array.prototype if it's not already there.
@DashK I am assuming its supported in firefox. Is that correct?
@chromedude: Yes (in fact, I linked to the MDC documentation for it in the answer)
3

This wraps it up into a convenient function:

function remove_element(array, item) {
 for (var i = 0; i < array.length; ++i) {
 if (array[i] === item) {
 array.splice(i, 1);
 return;
 }
 }
}
var array = ["google", "chrome", "os", "windows", "os"];
remove_element(array, "chrome");

or (for browsers that support indexOf):

function remove_element(array, item) {
 var index = array.indexOf(item);
 if (-1 !== index) {
 array.splice(index, 1);
 }
}

Edit: Fixed up with === and !==.

answered Sep 22, 2010 at 22:38

2 Comments

You should really be using === and !== instead of == and !=, respectively.
@Bears will eat you: No need for !== in (-1 !== index) ... index is known to be a number, because that's all indexOf will return, and -1 is known to be an number. So there's no need for strict comparison. I usually prefer something like (index > -1); YMMV.
2

Use the splice method of the Array class.

array.splice(1, 1);
answered Sep 22, 2010 at 22:34

Comments

2

The splice() method adds and/or removes elements to/from an array, and returns the removed element(s).

array.splice(indexOfElement,noOfItemsToBeRemoved);

in your case

 array.splice(1, 1);
answered Sep 22, 2010 at 22:41

Comments

2

You may want to remove all of the items that match your string, or maybe remove items that pass or fail some test expression. Array.prototype.filter, or a substitute, is quick and versatile:

var array= ["google","chrome","os","windows","os"],
b= array.filter(function(itm){
 return 'os'!= itm
});
alert(b)
T J
43.2k13 gold badges87 silver badges142 bronze badges
answered Sep 22, 2010 at 23:48

Comments

1

You didn't mention whether its required to retain the indices of the remaining elements in your array or not. On the basis that you can deal with having undefined members of an array, you can do:

var array = ["google","chrome","os","windows","os"];
delete array[1];

array[1] will then be undefined.

T J
43.2k13 gold badges87 silver badges142 bronze badges
answered Sep 22, 2010 at 23:30

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.