Javascript:Generic获取数组中的下一项

Javascript:Generic获取数组中的下一项,javascript,arrays,Javascript,Arrays,我正在尝试创建一个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",

我正在尝试创建一个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
var项=(来自用户输入)//从用户处获取初始项的一些代码
函数findcode(代码){
var arr=[“ball”、“1f7g”、“spoon”、“2c8d”、“pen”、“9c3c”];//制作数组

对于(var i=0;i您几乎完全正确,但语法是
arr[x]
,而不是
arr(x)

然后简单地

code = data[name]

我认为对象可能是这类任务的更好的数据结构

items = {
  ball : "1f7g",
  spoon: "2c8d", 
  pen  : "9c3c"
}


console.log(items['ball']); // 1f7g

您可以将数组作为参数传递给函数,并从函数返回找到的值:

var item = "spoon"; // from user input
var arr = ["ball", "1f7g", "spoon", "2c8d", "pen", "9c3c"]; //making the array
function findcode(item, arr){
    var idx = arr.indexOf(item);  //search array for whatever the user input was
    if(idx >=0 && idx <= arr.length - 2) { // check index is in array bounds
        return arr[i+1]; // return whatever comes next to item
    }
    return '';
}
document.write(findcode(item, arr)); //write the code, i.e., whatever comes after the item
String.prototype.cycle = function(arr) {
  const i = arr.indexOf(this.toString())
  if (i === -1) return undefined
  return arr[(i + 1) % arr.length];
};
Array.prototype.cycle = function(str) {
  const i = this.indexOf(str);
  if (i === -1) return undefined;
  return this[(i + 1) % this.length];
};
var item=“spoon”//来自用户输入
var arr=[“ball”、“1f7g”、“spoon”、“2c8d”、“pen”、“9c3c”];//制作数组
函数findcode(项目,arr){
var idx=arr.indexOf(item);//在数组中搜索用户输入的内容

如果(idx>=0&&idx从数组中循环项目,这可能很有用

const currentIndex = items.indexOf(currentItem);
const nextIndex = (currentIndex + 1) % items.length;
items[nextIndex];

第一个项目将从数组的开头开始,在最后一个项目之后

尝试此
字符串。prototype
函数:

var item = "spoon"; // from user input
var arr = ["ball", "1f7g", "spoon", "2c8d", "pen", "9c3c"]; //making the array
function findcode(item, arr){
    var idx = arr.indexOf(item);  //search array for whatever the user input was
    if(idx >=0 && idx <= arr.length - 2) { // check index is in array bounds
        return arr[i+1]; // return whatever comes next to item
    }
    return '';
}
document.write(findcode(item, arr)); //write the code, i.e., whatever comes after the item
String.prototype.cycle = function(arr) {
  const i = arr.indexOf(this.toString())
  if (i === -1) return undefined
  return arr[(i + 1) % arr.length];
};
Array.prototype.cycle = function(str) {
  const i = this.indexOf(str);
  if (i === -1) return undefined;
  return this[(i + 1) % this.length];
};
以下是您如何使用它:

"a".cycle(["a", "b", "c"]); // "b"
"b".cycle(["a", "b", "c"]); // "c"
"c".cycle(["a", "b", "c"]); // "a"
"item1".cycle(["item1", "item2", "item3"]) // "item2"
["a", "b", "c"].cycle("a"); // "b"
["a", "b", "c"].cycle("b"); // "c"
["a", "b", "c"].cycle("c"); // "a"
["item1", "item2", "item3"].cycle("item1") // "item2"
如果你想反过来做,你可以使用这个
数组。prototype
函数:

var item = "spoon"; // from user input
var arr = ["ball", "1f7g", "spoon", "2c8d", "pen", "9c3c"]; //making the array
function findcode(item, arr){
    var idx = arr.indexOf(item);  //search array for whatever the user input was
    if(idx >=0 && idx <= arr.length - 2) { // check index is in array bounds
        return arr[i+1]; // return whatever comes next to item
    }
    return '';
}
document.write(findcode(item, arr)); //write the code, i.e., whatever comes after the item
String.prototype.cycle = function(arr) {
  const i = arr.indexOf(this.toString())
  if (i === -1) return undefined
  return arr[(i + 1) % arr.length];
};
Array.prototype.cycle = function(str) {
  const i = this.indexOf(str);
  if (i === -1) return undefined;
  return this[(i + 1) % this.length];
};
以下是您如何使用它:

"a".cycle(["a", "b", "c"]); // "b"
"b".cycle(["a", "b", "c"]); // "c"
"c".cycle(["a", "b", "c"]); // "a"
"item1".cycle(["item1", "item2", "item3"]) // "item2"
["a", "b", "c"].cycle("a"); // "b"
["a", "b", "c"].cycle("b"); // "c"
["a", "b", "c"].cycle("c"); // "a"
["item1", "item2", "item3"].cycle("item1") // "item2"

+1仅用于添加,在较旧的浏览器上不起作用。@thg435非常感谢!这正是我所需要的。同时,也感谢对象替代方案。简单而有用的是,上一项的条件显然必须是(index>0&&index%项。长度
循环技巧-干净整洁。更短:
const nextIndex=+currentIndex%项。长度