1

I am trying to decrement a for/in loop in js.

I have found lots of instruction on decrementing a for-loop, but had no luck finding a statement that decrements a for/in loop.

var classRegister = ["Lawrence", "John", "Jeff", "Bobbi"];
for( var index in classRegister ) { 
 console.log(classRegister[index]);
}

This is my incrementing for/in loop statement that I want to modify to decrement.

3
  • 2
    Relevant: stackoverflow.com/q/500504/476 Commented Apr 13, 2019 at 7:08
  • Yea, it IS a bad idea to do this. Alas I do see people who DO do this. Commented Apr 13, 2019 at 7:10
  • Thanks Mark. The discussion in your link was very helpful. Commented Apr 13, 2019 at 8:38

3 Answers 3

4

Use basic for loop & avoid for..in to iterate an array

var classRegister = ["Lawrence", "John", "Jeff", "Bobbi"];
for (let i = classRegister.length - 1; i >= 0; i--) {
  console.log(classRegister[i]);
}

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

3 Comments

note, due to the falsy you can just use i instead of i >= 0
It's not what I was hoping for. But it'll work. Thank you!
Yet that wasn't the question.
2

If you want to use for/in you can, you simply need to subtract your index from the last index value.

var classRegister = ["Lawrence", "John", "Jeff", "Bobbi"];
for( var index in classRegister ) { 
 console.log(classRegister[classRegister.length - 1 - index]);
}

4 Comments

The question was simply HOW to do it in a for/in loop, not if there was a better way.
This does answer the question, but no-one is saying it is the right thing to do. :)k
Thank you Ralph. It looks like a solution that works around the limitation of for/in loops in js. Your answer as well as the other ones really help my understanding.
1

You should not use for..in for arrays instead use simple loop

var classRegister = ["Lawrence", "John", "Jeff", "Bobbi"];
for(let index = classRegister.length-1; index >= 0; index-- ) { 
 console.log(classRegister[index]);
}

Or a while loop

var classRegister = ["Lawrence", "John", "Jeff", "Bobbi"];
let index = classRegister.length-1;

while(index >= 0) { 
 console.log(classRegister[index]);
 index--;
}

There are hacky ways to do but you should never use them IMO

var classRegister = ["Lawrence", "John", "Jeff", "Bobbi"];

let copy = [...classRegister].reverse()  // this reverse array in place

for( var index in copy ) { 
 console.log(copy[index]);
}

11 Comments

If you are going to do it this way you need to reverse a copy of the array, otherwise it destructs the array order.
@RalphRitoch yeah you're right, changed to incorporate this too
Yea, you are right, my brain is asleep on that BUT consider while(index--) and avoid that index--; in the while... I think my brain is 'falsey" tonight also :)
Thank you all. I have very interesting options to play with now.
@BobbiLusic yes you can try things to learn, but as well as have a habit to follow good patterns so your code becomes better,readable and maintainable
|

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.