【问题标题】:Retrieving array values using indexOf使用 indexOf 检索数组值
【发布时间】:2013-06-07 20:14:35
【问题描述】:

我创建了一个如下所示的数组,

var array = ['hello', 'hi', 'good morning', 'sweet', 'cool', 'nice', 'hey!', 'how you doin?', 'Thanks!'];

var index = array.indexOf('good morning')//This will return 2 since 'good morning' is located in 2 in index.

我的问题是,如何循环数组中的所有 5 个值,从“早上好”开始,到“嘿!”结束?

我知道这可以通过使用 for 循环语句来轻松完成,如下所示:

for (x = 2; x<= 6; x++){
} 

但我正在寻找一种不同的方法来实现这一点,只需指定索引值为 2。

【问题讨论】:

  • 想做什么?你想提取这五个项目吗? array.slice(2,7)?
  • 可以使用array.slice() 取出数组的一部分。如果这就是你要找的。另外,很抱歉,我误读了您的问题,并投票决定关闭为重复。
  • 您在这里迭代时的最终目标是什么?现代 JS 中有大量方便的数组方法可能已经解决了整个问题。

标签: javascript arrays loops indexing


【解决方案1】:

不确定我是否明白,但要从 good morning 开始迭代并在 hey! 结束,只需使用字符串的索引作为循环中的开始和结束?

var arr = ['hello', 'hi', 'good morning', 'sweet', 'cool', 'nice', 'hey!', 'how you doin?', 'Thanks!'];

var start = arr.indexOf('good morning'),
    ends  = arr.indexOf('hey!');

for (var i=start; i<ends; i++) {
    console.log(arr[i]);
}

FIDDLE

你可以用slice()做类似的事情吗?

arr.slice(arr.indexOf('good morning'), arr.indexOf('hey!')).forEach(function(s) {
    console.log(s)
});

FIDDLE

【讨论】:

    【解决方案2】:
    for (var x = array.indexOf("good morning"); x < array.indexOf("hey!"); ++x)
    

    但老实说,我没有看到它的用途......

    【讨论】:

      【解决方案3】:

      根据您的具体问题,使用内置方法可能是一种更好的方法,但如果您真的想要一个带有 args 的更处理程序样式的迭代器来满足这个确切的需求,那么这里有一个如何扩展 Array 原型的示例。

      Array.prototype.disToDat = function(dis){
          if(dis===undefined || typeof dis === 'function'){
              throw new Error('No dis. You need dat and deserve to be dissed'); 
          }
      
          dis = this.indexOf(dis);
      
          if(typeof arguments[1] === 'function'){
              var dat = this.length - 1; //optional dat - runs to the end without
              var handler = arguments[1];
          }
          else if(typeof arguments[2] === 'function'){
              var dat = this.indexOf(arguments[1]);
              if(dis > dat){ throw new Error('Dat is before dis? What is dis!?'); }
      
              var handler = arguments[2];
          }
          else{
              throw new Error(
                  "You can't handle dis or dis and dat without a handler."
              );
          }
      
      
          if(dis === -1 || dat === -1){ return null; }
      
          for(var i=dis; i<=dat; i++){
              handler(this[i]);
          }
      }
      

      用法:

      ['a','b','c','d','e'].disToDat('b', 'd', function(disOne){ console.log(disOne); });
      //dis and dat
      
      ['a','b','c','d','e'].disToDat('b', function(disOne){ console.log(disOne); });
      //without optional dat, it just goes to the end
      

      引发错误:

      -缺少处理程序

      -第一个位置没有'dis'参数

      -'dis'之前的'dat'

      【讨论】:

        【解决方案4】:
        var array = ['hello', 'hi', 'good morning', 'sweet', 'cool', 'nice', 'hey!', 'how you doin?', 'Thanks!'];
        
        startIndex = array.indexOf('good morning');
        endIndex = array.indexOf('hey!');
        
        for (x = startIndex; x < endIndex; x++){
            console.log(array[x])
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-03-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多