【问题标题】:Javascript: Generic get next item in arrayJavascript:通用获取数组中的下一项
【发布时间】:2013-04-30 07:50:02
【问题描述】:

我正在尝试创建一个 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 新手,虽然这与我发现的许多其他问题相似,但这些问题似乎涉及更多的数组或更复杂的搜索。我似乎无法简化它们满足我的需要。)

【问题讨论】:

    标签: javascript arrays


    【解决方案1】:

    您几乎猜对了,但语法是 arr[x],而不是 arr(x)

    index = array.indexOf(value);
    if(index >= 0 && index < array.length - 1)
       nextItem = array[index + 1]
    

    顺便说一句,使用对象而不是数组可能是更好的选择:

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

    然后简单

    code = data[name]
    

    【讨论】:

    • +1 只是为了添加,Array.indexOf is ES5,不适用于旧版浏览器。
    • @thg435 非常感谢!这正是我所需要的。也感谢对​​象替代品。
    • 简单好用,很明显之前Item的条件必须是(index > 0 && index
    • 关于使用对象而不是数组,如果用户键入“ball”则效果很好——它返回“1f7g”。但是,如果用户搜索“1f7g”,则不会返回任何内容,因为数据对象中不存在该键。
    【解决方案2】:

    数组中的循环项目这可能很有用

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

    第一项将在最后一项之后从数组的开头取出

    【讨论】:

    • 喜欢% items.length 骑自行车的技巧 - 又好又干净。
    • 较短:const nextIndex = ++currentIndex % items.length
    • 我喜欢它。简单干净。
    【解决方案3】:

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

    items = {
      ball : "1f7g",
      spoon: "2c8d", 
      pen  : "9c3c"
    }
    
    
    console.log(items['ball']); // 1f7g
    

    【讨论】:

    • +1 我想知道 OP 是否知道这种行为是哈希/关联数组?
    【解决方案4】:

    试试这个String.prototype函数:

    String.prototype.cycle = function(arr) {
      const i = arr.indexOf(this.toString())
      if (i === -1) return undefined
      return arr[(i + 1) % arr.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"
    

    如果你想反过来做,你可以使用这个Array.prototype函数:

    Array.prototype.cycle = function(str) {
      const i = this.indexOf(str);
      if (i === -1) return undefined;
      return this[(i + 1) % this.length];
    };
    

    这是你如何使用它:

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

    【讨论】:

      【解决方案5】:

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

      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
      

      【讨论】:

        猜你喜欢
        • 2016-12-06
        • 1970-01-01
        • 1970-01-01
        • 2019-04-21
        • 1970-01-01
        • 2016-11-07
        • 2018-08-10
        • 2020-10-09
        • 2015-07-11
        相关资源
        最近更新 更多