Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 删除“;这";从数组中不知道索引?_Javascript_Arrays_Oop - Fatal编程技术网

Javascript 删除“;这";从数组中不知道索引?

Javascript 删除“;这";从数组中不知道索引?,javascript,arrays,oop,Javascript,Arrays,Oop,我有一个数组跟踪javascript对象,如下所示: var myArray = []; Main.prototype.myObject = function (x, y) { this.x = x; this.y = y; this.moveObject = function () { // Change x, y coordinates if (someEvent) { // Delete myself

我有一个数组跟踪javascript对象,如下所示:

var myArray = [];
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
    }
};
myArray.push(new main.myObject(this.x, this.y));
这些对象看起来像这样:

var myArray = [];
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
    }
};
myArray.push(new main.myObject(this.x, this.y));
将对象按如下方式推入阵列:

var myArray = [];
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
    }
};
myArray.push(new main.myObject(this.x, this.y));
现在,有没有一种方法可以使用
this
删除对象的特定实例,而不知道它在
myArray
中的索引


我希望保持for循环干净,并在已经存在的
moveObject()
函数中执行删除操作。

是的,您可以使用
.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);

是的,您可以使用
.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);

也许可以尝试以下方式:

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

也许可以尝试以下方式:

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

看看这个:你不能从对象本身中删除对象。你需要在数组的其他地方循环,找到你想要的对象,然后拼接这个对象或其他任何东西。你需要在其他地方的数组中循环,找到你想要的对象,然后拼接对象或其他任何东西。我想这就是OP的要求。。。但这很难做到tell@Oscar似乎无法让它工作。myArray.indexOf(myObject)杀死脚本,而myArray.indexOf(main.myObject)不做任何事情。log(index)总是返回-1。有什么线索吗?indexOf中的“myObject”应该是包含您正在查找的对象实例的实际变量。如果找不到放在.indexOf()中的变量,它将输出-1,这解释了console.log的输出。希望有帮助@这就是我的问题,对象是用新的main.myObject(x,y)创建的,这是我在for循环之外唯一的标识符。对象都是相同的。我仍然可以这样做,还是应该使用myArray[I]?哦,现在我想我理解了这个问题。在您的方法
deleteObject
中,您正在myArray中查找
对象的索引?在这种情况下,实际上可以将
this
传递给.indexOf(),因为
this
是对实例本身的引用。这有意义吗?函数是这样的:我想这就是OP所要求的。。。但这很难做到tell@Oscar似乎无法让它工作。myArray.indexOf(myObject)杀死脚本,而myArray.indexOf(main.myObject)不做任何事情。log(index)总是返回-1。有什么线索吗?indexOf中的“myObject”应该是包含您正在查找的对象实例的实际变量。如果找不到放在.indexOf()中的变量,它将输出-1,这解释了console.log的输出。希望有帮助@这就是我的问题,对象是用新的main.myObject(x,y)创建的,这是我在for循环之外唯一的标识符。对象都是相同的。我仍然可以这样做,还是应该使用myArray[I]?哦,现在我想我理解了这个问题。在您的方法
deleteObject
中,您正在myArray中查找
对象的索引?在这种情况下,实际上可以将
this
传递给.indexOf(),因为
this
是对实例本身的引用。这有意义吗?下面是函数的外观: