Link to home
Start Free TrialLog in
Avatar of trinh
trinh

asked on

how to reindex the array

Is there a way to repack the index once you delete an
element of the array. This leads to a ever growing array
since my application is adding and deleting
elements of the array. Without repack, there will be holes
in the array.
Avatar of sybe
sybe

array.sort()

will reindex the array. You can define your own compare function

array.sort(myFunction)

function myFunction(a,b) {
  return a-b;
}
Avatar of trinh

ASKER

No sort won't reindex the array. I try it and still get the error.
after deleting an element of the array, I sort the array then go through
the loop to display the array contents, an error occurs when it reaches the
empty array element that has just been deleted. (error is:
myarray[...].element is not an object). Mind you I am using 2 dimentional array
ASKER CERTIFIED SOLUTION
Avatar of sybe
sybe

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of trinh

ASKER

I can see your trick, certainly it will work but for a large 2 dimentional array it is
a bit ugly. By the way, how do you sort the array to make sure that the deleted
element will always be the last one ?