gpt4 book ai didi

javascript - 如何在 if 语句中使用数组变量?

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

var n = new Array();
n[0] = "zero";
n[1] = "first";
n[2] = "second"

function myFunction() {
var x = prompt("Question?")
if (x.toLowerCase() == n) {
//code
} else {
//code
}
}

是否可以使得如果输入任何数组变量,if/else 函数仍然执行。

最佳答案

猜测您想检查数组中是否存在键入的值?如果是这样:

function myFunction() {
var x = prompt("Question?")
if (n.indexOf(x.toLowerCase()) > -1) {
//code
} else {
//code
}
}

如果您需要支持 IE8 及更早版本,则需要 Array.prototype.indexOf 的填充程序(例如 the one provided by MDN )。

关于javascript - 如何在 if 语句中使用数组变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15511202/

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