gpt4 book ai didi

javascript - 搜索大量数组

转载 作者:行者123 更新时间:2023-11-30 18:51:36 25 4
gpt4 key购买 nike

嘿,我正在分别搜索每个数组以获取用户的特定输入。

if ($.inArray(i.val(), helloInputArray) > -1) { //IF HELLO
if (hello == 0) { //HAVE YOU ALREADY SAID HI?
r = Math.floor(Math.random()*4);
o.html(o.html()+helloOutputArray[r]);
hello = 1;
i.val('');
} else { //IF YOU'VE ALREADY SAID HI...
o.html(o.html()+'I already said hi to you!<br />');
i.val('');
}
} else if ($.inArray(i.val(), byeInputArray) > -1) { //IF GOODBYE
if (bye == 0) {
r = Math.floor(Math.random()*4);
o.html(o.html()+byeOutputArray[r]);
i.val('');
} else {
o.html(o.html()+'I already said goodbye... Go away!');
i.val('');
}
}

有什么方法可以一次搜索所有数组,因为我需要在每个数组中搜索一个字符串。

咳咳

所以 - 如果我输入“ae”,那么我希望它遍历每个数组中的每个项目并返回其中包含“ae”的所有字符串。

^_^ <(措辞不当...)

最佳答案

如果我没理解错的话,您想将单独的数组合并为一个以执行一个循环,但又不想修改原始数组。

如果是这样,试试这个:

if( $.inArray( i.val(), helloInputArray.concat( byeOutputArray) ) > -1 ) {
...

The .concat() method将创建连接在一起的两个数组的副本,因此不会修改原件。该副本作为第二个参数传递给 $.inArray() 方法。


编辑:

从您下面的评论来看,您似乎想测试 i.val() 是否是数组中任何项目的子字符串。

如果是这样,您可能不会使用 $.inArray,而是使用 $.each() 来迭代数组,并测试值(value)。

var isInArray = false;
var value = i.val();

$.each( helloInputArray.concat(byeOutputArray), function(i,val) {
if( val.indexOf( value ) > -1 ) {
isInArray = true;
return false;
}
});

if ( isInArray ) > -1) {
if (hello == 0) {
r = Math.floor(Math.random()*4);
o.html(o.html()+helloOutputArray[r]);
...

关于javascript - 搜索大量数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3718690/

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