gpt4 book ai didi

javascript - 使用 javascript 中的 splice 删除嵌套数组中的项目

转载 作者:行者123 更新时间:2023-12-03 11:28:03 25 4
gpt4 key购买 nike

我在使用 splice 从数组中删除项目时遇到问题。首先,我有一个名为player的数组,其中包含另外三个名为tier1、tier2和tier3的数组。所有这三个数组都存储了我想要操作的值。我的页面上有一个输入和一个按钮,单击该按钮会激活与拼接数组相关的 JS 函数。我已经使用 FOR 循环设置了一个 IF 语句,以便查看 tier1 以检查输入文本是否与数组中的值匹配,如果在 tier1 中找不到文本,则应该对 tier2 执行相同的操作,然后是第 3 层,最后如果输入文本不在任何数组内,则显示错误消息。如果输入文本位于 tier1 中,该函数可以正常工作,但由于某种原因,它似乎没有运行 else if 语句,我在下面提供了我的代码,我不确定这是否只是一个语法错误,或者我是否遗漏了一些某种功能。非常感谢提前

var player=[]
var tier1=["p1","p2","p3"]
var tier2=["p4","p5","p6","p7"]
var tier3=["p8","p9","p10","p11"]

function removeFunction() {
if (document.getElementById("removePlayer").value !== "") {
for (index = 0; index < tier1.length; index++) {
if(document.getElementById("removePlayer").value == tier1[index]) {
tier1.splice(index, 1);
playerFunction();
playerAlert = "Player Removed";
document.getElementById("test1").innerHTML = playerAlert;
document.getElementById("test2").innerHTML = tier1;
}}
} else if (tier1[index] !== document.getElementById("removePlayer").value) {
for (index = 0; index < tier2.length; index++) {
if(tier2[index] == document.getElementById("removePlayer").value) {
tier2.splice(index, 1);
playerFunction();
playerAlert = "Player Removed";
document.getElementById("test1").innerHTML = playerAlert;
document.getElementById("test2").innerHTML = tier2;
}}
} else if (tier2[index] !== document.getElementById("removePlayer").value) {
for (index = 0; index < tier3.length; index++) {
if(tier3[index] == document.getElementById("removePlayer").value) {
tier3.splice(index, 1);
playerFunction();
playerAlert = "Player Removed";
document.getElementById("test1").innerHTML = playerAlert;
document.getElementById("test2").innerHTML = tier3;
}
}} else {
playerAlert = "Nothing happened";
document.getElementById("test1").innerHTML = playerAlert;
}
}

http://jsfiddle.net/9r73ntsc/1/

最佳答案

这会对你有帮助

function removeFunction() {
if (document.getElementById("removePlayer").value !== "") {
for (index = 0; index < tier1.length; index++) {
if (document.getElementById("removePlayer").value == tier1[index]) {
tier1.splice(index, 1);
playerFunction();
playerAlert = "Player Removed";
document.getElementById("test1").innerHTML = playerAlert;
document.getElementById("test2").innerHTML = tier1;
return;
}
if (document.getElementById("removePlayer").value == tier2[index]) {
tier2.splice(index, 1);
playerFunction();
playerAlert = "Player Removed";
document.getElementById("test1").innerHTML = playerAlert;
document.getElementById("test2").innerHTML = tier2;
return;
}
if (document.getElementById("removePlayer").value == tier3[index]) {
tier3.splice(index, 1);
playerFunction();
playerAlert = "Player Removed";
document.getElementById("test1").innerHTML = playerAlert;
document.getElementById("test2").innerHTML = tier3;
return;
}
}
}
playerAlert = "Nothing happened";
document.getElementById("test1").innerHTML = playerAlert;
}

编辑:将最终 IF 语句从 tier2[index] 更正为 tier3[index]

关于javascript - 使用 javascript 中的 splice 删除嵌套数组中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26844316/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com