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.

Code Maniac
  • 37,143
  • 5
  • 39
  • 60

3 Answers3

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]);
}
brk
  • 48,835
  • 10
  • 56
  • 78
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]);
}
Ralph Ritoch
  • 3,260
  • 27
  • 37
  • Check this https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in#Array_iteration_and_for...in – brk Apr 13 '19 at 07:07
  • The question was simply HOW to do it in a for/in loop, not if there was a better way. – Ralph Ritoch Apr 13 '19 at 07:08
  • 1
    This does answer the question, but no-one is saying it is the right thing to do. :)k – Mark Schultheiss Apr 13 '19 at 07:08
  • 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. – Bobbi Lusic Apr 13 '19 at 08:37
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]);
}
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
  • If you are going to do it this way you need to reverse a copy of the array, otherwise it destructs the array order. – Ralph Ritoch Apr 13 '19 at 07:11
  • @RalphRitoch yeah you're right, changed to incorporate this too – Code Maniac Apr 13 '19 at 07:13
  • `while(index)` is functionally the same as `while(index >= 0)` – Mark Schultheiss Apr 13 '19 at 07:17
  • @MarkSchultheiss no it is not, `index>=0` will not take you out of while loop at `index = 0` on the other hand `while(index)` will take you out of while loop at `index = 0` – Code Maniac Apr 13 '19 at 07:19
  • @MarkSchultheiss `while(index)` and `while(index > 0 )` will be same in functionality not `while(index >= 0)` – Code Maniac Apr 13 '19 at 07:22
  • 1
    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 :) – Mark Schultheiss Apr 13 '19 at 07:23
  • @MarkSchultheiss yes that can be done, but i prefer this for sake of readality so you can easily see `oh this while loop run untill the value is greater than 0`, this line is `I think my brain is 'falsey" tonight also :)` awesome pun intended – Code Maniac Apr 13 '19 at 07:25
  • In older browsers (way older) the negative while is faster than a for loop, it is able to short cycle, in newer ones that has gone away for the most part.. But I digress here – Mark Schultheiss Apr 13 '19 at 07:27
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/191775/discussion-between-code-maniac-and-mark-schultheiss). – Code Maniac Apr 13 '19 at 07:32
  • 1
    Thank you all. I have very interesting options to play with now. – Bobbi Lusic Apr 13 '19 at 08:42
  • 1
    @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 – Code Maniac Apr 13 '19 at 08:49