-1

I have an array in javascript that looks like this:

arr = ["md51234","md55234"]

I'm trying to remove an item from this by doing:

delete arr["md51234"]

But this doesn't seem to work. Is there another way to remove this?

@dystroy provided the answer, I added indexOf to the array prototype for non-compliant browsers:

if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function(obj, start) {
         for (var i = (start || 0), j = this.length; i < j; i++) {
             if (this[i] === obj) { return i; }
         }
         return -1;
    }
}
Paul Fleming
  • 22,772
  • 8
  • 65
  • 107
dzm
  • 20,376
  • 41
  • 127
  • 215
  • possible duplicate of [Remove item from array by value](http://stackoverflow.com/questions/3954438/remove-item-from-array-by-value) – I Hate Lazy Oct 25 '12 at 20:01
  • Your indexOf shim has some incompatibilities. For `i`, you should do a toNumber conversion of `start` and some other tests to ensure it still works if some non valid number is passed. And your `if` condition should first include an `in` test for `i`. A more complete shim can be found here. https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/indexOf#Compatibility – I Hate Lazy Oct 25 '12 at 22:23

2 Answers2

4

You must provide the index, not the value :

delete arr[0];

Alternatively, you could also use indexOf on most browsers

delete arr[arr.indexOf("md51234")];

But note that delete doesn't make the array shorter, it just make a value undefined. Your array after having used delete is

[undefined, "md55234"]

If you want to make the array shorter, use

arr.splice(0, 1); // first parameter is index of element to remove, second one is number of elements to  remove

This makes

["md55234"]
Denys Séguret
  • 335,116
  • 73
  • 720
  • 697
  • or, if you realy want to delete by the string declare it as a Map: `arr = {"md51234": "md51234", "md55234": "md55234"}` – melanke Oct 25 '12 at 20:05
  • So that would make `arr.splice(arr.indexOf('md51234'), 1)` – J. K. Oct 25 '12 at 20:23
  • @IanKuca Yes. But usually you would do it in two steps, so that you're sure the index isn't -1 (i.e. the string *is* in the array). – Denys Séguret Oct 25 '12 at 20:24
  • 1
    Yep, I'm just clarifying the splice call in case the OP doesn't know which argument is which. The first one is the index from where to start removing items, the second one is the number of items to remove. – J. K. Oct 25 '12 at 20:29
  • @IanKuca I hadn't thought about it, you're right it's better explicited. I added a comment – Denys Séguret Oct 25 '12 at 20:30
  • Perfect, thank you. I've added indexOf to the array prototype for other browsers. – dzm Oct 25 '12 at 22:04
-1

Different approach using jQuery:

arr = ["a", "b", "c", "d", "e"];

Remove item by index:

arr = jQuery.grep(arr, function(value, index) {
    return index != 2;
});

Remove item by value:

arr = jQuery.grep(arr, function(value, index) {
    return value != "a";
});
Reflective
  • 3,100
  • 1
  • 10
  • 21
  • 1
    Installing jquery to remove an object from a array is a little like renting a Cray when you want to compute 2+2 – Denys Séguret Oct 25 '12 at 20:25
  • Using pure javascript on a regular project is like hanging on a tree ... normally jQuery is a required part of a project ... anyway it is just an example of nonstandart way of removing an item from an array ... without need of compacting it after removal ... you can remove more than 1 item ... and etc. commonly used in Perl and other languages having collection iterators – Reflective Oct 25 '12 at 20:28
  • -1 for what @dystroy said, plus the fact that you're not actually removing anything from the original array. – I Hate Lazy Oct 25 '12 at 20:31
  • you are funny guys :) you have a way to remove an item by index, by value, remove more than one item at once, to have any kind of more complex condition to remove as many items as you like, to use it in many other ways :) at least you can vote -10 :) yes it is a coolect kind of method but it is doing the same as remove. – Reflective Oct 25 '12 at 20:39
  • 1
    @Reflective: It is absolutely not doing the same thing. Replacing an Array is not the same as mutating an Array. I know it's delving into that scary "pure javascript" realm, but Arrays are Objects, and Objects are Reference Types. All other references to the original Array will not observe the change. – I Hate Lazy Oct 25 '12 at 20:50
  • being 24 years in programming - nothing is scary my friend :) But you should answer yourself the question: Why jQuery is made for? If everything with JavaScript was perfect there will be no jQuery and so frequently used! Yes the item is not phisicaly removed from the array, but finally you have na array with removed items. Normally not referenced objects are garbade collected. – Reflective Oct 25 '12 at 21:03
  • jQuery is primarily a layer over the DOM API to smooth out browser incompatibilities. Loading it for this purpose makes little sense. If you were to use a replace approach, you'd probably want to use native code or shims instead of a non-standard API. `arr = arr.filter(func...)` But you'd still have the issue of needing to update all references to the original Array. If there was only one reference, it's a non-issue. If not, it's a serious issue. – I Hate Lazy Oct 25 '12 at 21:12
  • Could you just try the example and see what finally is refferenced in `arr`? – Reflective Oct 25 '12 at 21:15
  • 1
    Once again, if there's only one reference, it's a non-issue. That one reference would obviously be by the `arr` variable, and it would be successfully updated. If there are multiple references, you have an issue. The `arr` variable would observe the change, but the other variables/properties that are referencing the Array object would not. They would still have reference to the original Array, causing the data to be out of sync. – I Hate Lazy Oct 25 '12 at 21:44
  • Yes I understand what you are talking about ... `var arrcpy = arr; ... arr = arr.filert( ...);` - `arrcpy` will keep reference to the original array ... yes it's true ... no doubt ... but actually this is an advantage ... not disadvantage ... but i can agree that it depends on the point of view – Reflective Oct 25 '12 at 22:09
  • Yes exactly. And yes, it could be an advantage if that's what the code calls for. But if someone is asking how to remove an item from an Array, without knowing more about the code it's a little dangerous to show them a technique that replaces the Array instead. – I Hate Lazy Oct 25 '12 at 22:14
  • yes it could be ...but we have a sentence: The one who's affraid of bears, don't have to go in the forest. But yes, I agree it could be dangerous for a beginner. If we follow the question in alphabet this is not an exact answer but I'm sure it gives a bit more knowledge and if it is used in corect way can solve many similar cases. Grep is very frequently used in Perl for example. Smalltalk have similar collect method on many type of collections ... and many other examples. This issue with deleting the value but still keepinng a null pointer exists from years ...and nobody takes care of it. – Reflective Oct 25 '12 at 22:27
  • `arr.splice(index, 1)` is the exact answer but it was already told by another answer :) Just wanted to add a bit of color to the exact answer :) – Reflective Oct 25 '12 at 22:38
  • If you're talking about the `delete` solution in the other answer, the ramifications were explained there. Even given that situation where we end up with a hole in the Array, the native `Array.prototype` methods generally pass over those Array holes... something `$.grep` doesn't do. But `delete` isn't needed anyway. A `.splice(idx, 1)` will reindex the Array. EDIT: ...as your new comment states. :) – I Hate Lazy Oct 25 '12 at 22:39