0

I have an array keeping track of javascript objects like this:

var myArray = [];

The objects looks something like this:

Main.prototype.myObject = function (x, y) {
    this.x = x;
    this.y = y;

    this.moveObject = function () {
        // Change x, y coordinates
        if (someEvent) {
            // Delete myself
            // deleteObject();
        }
    };
    this.deleteObject = function () {
        // Delete code
    }
};

And the objects are pushed into the array like this:

myArray.push(new main.myObject(this.x, this.y));

Now, is there a way I can delete a specific instance of the object with this without knowing it's index within myArray?

I'd rather keep the for loop clean and do the deleting in the already existing moveObject() function.

justanotherhobbyist
  • 2,124
  • 4
  • 28
  • 38
  • Take a look at this one: http://stackoverflow.com/questions/143847/best-way-to-find-an-item-in-a-javascript-array – Jonathan M Jul 30 '14 at 20:38
  • You can't delete an object from within itself. You need to loop through the array elsewhere, find the object you want, and then splice the object or whatever. – HartleySan Jul 30 '14 at 20:41

2 Answers2

4

Yes, you can request the index using .indexOf:

//  find the index in the array (-1 means not found):
var index = myArray.indexOf(myObj);

//  remove the element at that index, if found:
if(index > -1) myArray.splice(index, 1);
Oscar
  • 668
  • 6
  • 12
  • I think that's what the OP is asking... but it's hard to tell – Ruan Mendes Jul 30 '14 at 20:41
  • @Oscar Can't seem to get it to work. myArray.indexOf(myObject) kills the script, and myArray.indexOf(main.myObject) doesn't do anything. And console.log(index) always returns -1. Any clues what it could be? – justanotherhobbyist Jul 30 '14 at 20:56
  • The 'myObject' in the indexOf should be the actual variable that contains the instance of the object you're looking for. If it can not find the variable you put within .indexOf(), it will output -1, which explains your console.log output. Hope that helps! – Oscar Jul 30 '14 at 20:58
  • @Oscar Well that's my problem, the objects are created with new main.myObject(x, y) and that's the only identifier I have outside of the for loop. The objects are all the same. Can I still do this or should I just go with myArray[i]? – justanotherhobbyist Jul 30 '14 at 21:01
  • Oh, now I think I understand the problem. Within your method `deleteObject` you're looking for the index of the `this` object in the myArray array? In that case you can actually pass `this` to the .indexOf(), as `this` is a reference to the instance itself. Does that make sense? Here's what the function would look like: http://jsfiddle.net/m65Jr/ – Oscar Jul 30 '14 at 21:04
  • @Oscar Fantastic, that solves it. I tried with `this` earlier but probably didn't structure it correctly. Your jsFiddle did the trick thanks. – justanotherhobbyist Jul 30 '14 at 21:10
2

Maybe try something like:

this.deleteObject = function() {
    var idx = myArray.indexOf(this);
    if (idx >= 0) {
        myArray.splice(idx, 1);
    }
}
Jim
  • 107
  • 8