删除“this"从不知道索引的数组
Delete "this" from array without knowing index?
我有一个数组跟踪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));
现在,有没有一种方法可以删除具有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);
也许可以试试:
this.deleteObject = function() {
var idx = myArray.indexOf(this);
if (idx >= 0) {
myArray.splice(idx, 1);
}
}
相关文章:
- 元素在我的代码中不会.fadeTo.Don'I don’我不知道;s错误的JavaScript、JQuery、H
- 我可以'我似乎不知道如何修复javascript中的两个lint.有人能帮我理解吗
- 在不知道深度或父属性的情况下从对象中删除属性
- Steam Web API:I'我不知道该如何处理这个看起来像哈希的url来生成图像
- 使用.on动态添加jquery/js不知道的html元素
- 这个旋钮没有更新;我不知道为什么
- 我不知道为什么我的代码是错误的?又有什么错
- 如何在不知道关键字的情况下访问javascript对象值
- 我不知道我的编码有什么问题.(JavaScript)
- 我正在尝试使用多个2数组来绑定svg圆的数据,但不知道如何绑定
- I'我不知道为什么我的代码没有'不起作用
- sqlite查询返回错误-can'我不知道为什么
- 我不知道此代码中的这些符号是什么意思.十进制到二进制
- 关于“索引”,我认为它是对的,当它是错的.我不知道为什么
- Javascript:在数组中查找对象的索引,不知道索引,只知道对象
- 在不知道索引的情况下选择jquery tools选项卡
- Indexeddb:在运行前不知道索引名时,从多个索引中获取数据
- 不断得到未定义的索引错误,不知道如何声明值在子模态窗口
- 如何在不知道JQuery索引的情况下访问特定的列表项
- 得到一个未定义的索引错误,不知道为什么完全不知道为什么,似乎找不到任何其他解决方案