Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how to reindex the array

Posted on 1998-09-11
4
Medium Priority
?
1,760 Views
Last Modified: 2012-08-14
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.
0
Comment
Question by:trinh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 28

Expert Comment

by:sybe
ID: 1270763
array.sort()

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

array.sort(myFunction)

function myFunction(a,b) {
  return a-b;
}
0
 

Author Comment

by:trinh
ID: 1270764
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
0
 
LVL 28

Accepted Solution

by:
sybe earned 60 total points
ID: 1270765
Hmm, I worked with a 1 dimensional array, and solved something comparable by sorting the array in such a way that (aftere each "delete") the empty element was the last one, then copying all elements except the last one to a temporary array and then copying the temporary array back to the original. Bit ugly, but it works.

0
 

Author Comment

by:trinh
ID: 1270766
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 ?
0

Featured Post

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question