31

Suppose I have an array of objects called MyArray and that a certain function returns a reference for a particular element within that array; something like this:

MyArray = [Object1, Object2, ..., Objectn];

function DoWork() {

   var TheObject = GetTheObject(SomeParamter);
}

At this point, TheObject points to a certain element in the array. Suppose I want to remove this element from MyArray, is this possible without having to reloop through the array to get the index of the element?

I'm looking for something like splice that would work with the reference to the element rather than the index of the element.

The Codesee
  • 3,714
  • 5
  • 38
  • 78
frenchie
  • 51,731
  • 109
  • 304
  • 510
  • possible duplicate: http://stackoverflow.com/questions/3396088/how-do-i-remove-an-object-from-an-array-with-javascript – achudars Jul 15 '13 at 15:19
  • 1
    Writing `TheObject = null` will not clear the object from the array. You have it wrong. – Jon Jul 15 '13 at 15:19
  • 2
    arr.splice(arr.indexOf(obj), 1); – dandavis Jul 15 '13 at 15:20
  • If an object should be removed form an array, there is no way around that you or the engine loops over the array. What you can think about is to add an optional parameter to `GetTheObject` that will remove if `true` or keep if `false`. – t.niese Jul 15 '13 at 15:21
  • @dandavis: you got it – frenchie Jul 15 '13 at 15:21
  • @acudars: no, it's not a duplicate at all; it's about using splice when you DON'T have the index – frenchie Jul 15 '13 at 15:21

1 Answers1

50

Simply use Array.prototype.indexOf:

let index = MyArray.indexOf(TheObject);
if(index !== -1) {
  MyArray.splice(index, 1);
}

Keep in mind that if targeting IE < 9 you will need to introduce a polyfill for indexOf; you can find one in the MDN page.

NoNameProvided
  • 8,608
  • 9
  • 40
  • 68
Jon
  • 428,835
  • 81
  • 738
  • 806
  • 1
    where did remove come from? – dandavis Jul 15 '13 at 15:21
  • Ok, then if it doesn't work in IE8 I'll still accept the answer and will keep it in mind for later even though I have to reloop since IE8 support is a must for now. – frenchie Jul 15 '13 at 15:24
  • 2
    @frenchie: `indexOf` reloops in all browsers anyway. I didn't mean to suggest it somehow determined what the index is out of thin air. – Jon Jul 15 '13 at 15:25
  • 1
    This doesn't answer the question. OP asked to remove an element by reference and the answer does not do that, it removes it by index. – so001 Sep 14 '19 at 14:24
  • @tshm001: It does answer the question. Yes, the actual method call that removes the item requires an index, but the index is calculated automatically based on the reference you pass in. So from the caller's perspective, it effectively removes by reference. – Jon Sep 15 '19 at 00:55
  • Jon, the reason this doesn't actually remove by reference is because if you for instance have two separate arrays, Array A and Array B, you cannot simply do ArrayA.indexOf(MyObject). To actually remove the object by reference in actuality would be similar to `delete MyObject` where `delete` knows what array MyObject belongs to, and as requested, actually deletes the object by reference alone. – so001 Sep 16 '19 at 20:04
  • 1
    @tshm001: Uh, how does multiple arrays make sense in the context of this question, or even any context at all? Speaking about this question, it clearly says "I want to remove this element from `MyArray`" -- there's no ambiguity there. In a more general context, how would it be possible for any language or library to offer a "remove this object in whatever container it might be in" unless you explicitly code it to search a given set of containers? It makes absolutely no sense. – Jon Sep 17 '19 at 13:05
  • It makes perfect sense if you are able to *delete the object by reference* rather than *delete the object by index*. Of course, you cannot find the index when you do not know the exact array that the object is in. Which is why this code deletes the object by index, rather than reference. – so001 Sep 17 '19 at 21:56
  • @tshm001 no, and I give up. – Jon Sep 18 '19 at 01:14
  • Think of a nested array of objects, with some containing properties like "children" to contain child arrays of the same kind of objects. This method doesn't remove if you have reference to an object which you don't know the containing array. – Taha Paksu Nov 19 '19 at 21:32