我正在尝试制作一个 JavaScript 函数,它将在字符串数组中搜索一个值并返回下一个字符串。例如,如果构建了一个数组,其中一个项目后跟其股票代码,我想搜索该项目并编写股票代码。
var item = (from user input); //some code to get the initial item from user
function findcode(code){
var arr = ["ball", "1f7g", "spoon", "2c8d", "pen", "9c3c"]; //making the array
for (var i=0; i<arr.lenth; i++){ //for loop to look through array
arr.indexOf(item); //search array for whatever the user input was
var code = arr(i+1); //make the variable 'code' whatever comes next
break;
}
}
document.write(code); //write the code, I.e., whatever comes after the item
(我确信很明显我是 JavaScript 新手,虽然这与我发现的许多其他问题相似,但这些问题似乎涉及更多的数组或更复杂的搜索。我似乎无法为我的需要。)