最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How can I skip a specific Index in an array in a for loop javascript - Stack Overflow

programmeradmin3浏览0评论

Suppose I had a function that is pulling in values from somewhere and storing those values into an array.

function getSport(ply) {
   some code here... //function gets values that I need for array later
}

var sports1 = getSport(playerChoice); 
var sports2 = getSport(playerChoice);
var sports3 = getSport(playerChoice);
var sports4 = getSport(playerChoice);

var sportsArry = [sports1, sports2, sports3, sports4];

Now I would like to use a for loop to loop the elements, the problem, however, is the first index (index 0) will always be true. I want to skip index 0. How do I do that? Further I want to replace index 0 with something else. Let me show you

for (var i = 0; i<sportsArry.length; i++){
  if ( (sports1 == sportsArry[i])  ) {
     sports1 = null;   //I figured I should null it first?
     sports1 = replaceValueFunc(playerChoice2);
  }
}  

Well you can see the problem I would have. Index 0 is true.

Let me show you what would work, although it requires alot of or operators.

if ( (sports1 == sportsArry[1]) || (sports1 == sportsArry[2]) || (sports1 == sportsArry[3] ) {

... }

^^ That is one way to skip index 0, what would be another better looking way?

Suppose I had a function that is pulling in values from somewhere and storing those values into an array.

function getSport(ply) {
   some code here... //function gets values that I need for array later
}

var sports1 = getSport(playerChoice); 
var sports2 = getSport(playerChoice);
var sports3 = getSport(playerChoice);
var sports4 = getSport(playerChoice);

var sportsArry = [sports1, sports2, sports3, sports4];

Now I would like to use a for loop to loop the elements, the problem, however, is the first index (index 0) will always be true. I want to skip index 0. How do I do that? Further I want to replace index 0 with something else. Let me show you

for (var i = 0; i<sportsArry.length; i++){
  if ( (sports1 == sportsArry[i])  ) {
     sports1 = null;   //I figured I should null it first?
     sports1 = replaceValueFunc(playerChoice2);
  }
}  

Well you can see the problem I would have. Index 0 is true.

Let me show you what would work, although it requires alot of or operators.

if ( (sports1 == sportsArry[1]) || (sports1 == sportsArry[2]) || (sports1 == sportsArry[3] ) {

... }

^^ That is one way to skip index 0, what would be another better looking way?

Share Improve this question asked Feb 10, 2015 at 9:24 dragonoredragonore 8032 gold badges12 silver badges26 bronze badges 1
  • 1 Can't you just start your loop at 1 ? – Dimitar Dimitrov Commented Feb 10, 2015 at 9:27
Add a ment  | 

1 Answer 1

Reset to default 6

I want to skip index 0. How do I do that? Further I want to replace index 0 with something else.

Just start the loop from 1 instead of 0

sportsArr[0] = "Something else"; // set the first element to something else
for(var i = 1; i < sportsArr.length; i++){ 
   // do something
}
发布评论

评论列表(0)

  1. 暂无评论