0

I need to loop through an array using the for clause, but starting at some specific index and just to a maximum of iterations.

The code below does the task, but it looks awful to me: it's there a better way?

  var offset = 10, max = 5;
  for (var i = 0; (i + offset) < data.length && i < max; i++) {
    doSomething(data[i + offset]);
  }

1 Answer 1

3

If I am understanding your question correctly you would just need to initialize i to the offset.

var offset = 10, max = 5 + offset;
for (var i = offset; i < data.length && i < max; i++) {
    doSomething(data[i]);
}

edit: didn't understand the max at first.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.