Need to reset just the indexes of a Javascript array

Adnan Baliwala picture Adnan Baliwala · Jul 10, 2012 · Viewed 16.3k times · Source

I have a for loop which returns an array.

Return:

1st loop:
arr[0]
arr[1]
arr[2]
arr[3]

Here the length I get is 4 (Not a problem).

Return:

2nd loop
arr[4]
arr[5]
arr[6]
arr[7] 
arr[8] 

Here the length I get is 9.

What I want here is the actual count of the indexes i.e I need it to be 5. How can I do this. And is there a way that when I enter each loop every time it starts from 0 so that I get proper length in all the loops?

Answer

Dag Sondre Hansen picture Dag Sondre Hansen · Dec 14, 2013

This is easily done natively using Array.filter:

resetArr = orgArr.filter(function(){return true;});