![](/img/trans.png)
[英]How to remove specific element from element array in javascript or jquery
[英]How can I remove an element from an array with javascript / jQuery?
我在这样的数组中设置一个元素:
dialogs[id] = $.modal({
title: "Admin",
closeButton: true,
content: content,
width: false,
resizeOnLoad: true,
buttons: {
'Close': function (win) {
win.closeModal();
}
}
}).find('form') // Attach logic on forms
.submit(formSubmitHandler)
.end();
稍后我检查是否存在这样的内容:
if (!dialogs[id]) {
loadAndShowDialog(id, link, url);
}
如何从对话框数组中删除id
记录? 是否有类似dialogs[id].Remove()
?
命令为:(将其设置为undefined)
delete dialogs[id];
如果要完全删除:请使用splice
。
我错误地认为这是您要删除的对象属性(在这里删除将是很好的-唯一的解决方法)
但是-您有一个数组,正确的答案是使用splice
。
您可以使用
delete dialogd[id]; //will not remove element, will set it to undefined
要么
dialogs.splice(id,1); //will not work in case of non-numeric indices
选择合适的一个。 我喜欢第二个。
从数组中删除项目时,请使用splice
dialogs.splice(id,1); //remove "1" item starting from index "id"
请注意,这将删除该项目,并更改数组长度。 因此在3的数组中,如果以这种方式进行拼接,则数组的长度现在为2。
使用delete
不会影响数组,但是会将该位置保留为undefined
。 就像在那个位置“未设定”或“留下一个洞”。
delete dialogs[id]; //value at index "id" is now undefined
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.