Click here to Skip to main content
15,443,631 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
If you delete the array while using the array data, there is a hole in the middle. Deleting using splice automatically sorts it, but doing so affects the internal values ​​of the array, so I'm looking for another way. Is there a way to reorder the index numbers sequentially?

What I have tried:

page: Array(2)
  1: Array(5)
    ->0: {s: 20, d:442.....}
    ->2: {s: 20, d:442.....}
    ->4: {s: 20, d:442.....}
  2: Array(5)
    ->0: {s: 20, d:442.....}
    ->1: {s: 20, d:442.....}
    ->4: {S: 20, d:442.....}

To
JavaScript
page: Array(2)
  1: Array(5)
    ->0: {s: 20, d:442.....}
    ->1: {s: 20, d:442.....}
    ->2: {s: 20, d:442.....}
  2: Array(5)
    ->0: {s: 20, d:442.....}
    ->1: {s: 20, d:442.....}
    ->2: {S: 20, d:442.....}
Posted
Comments
Richard MacCutchan 1-Jun-22 3:55am    
You should not add or remove array items while you are processing looping through the array. If you want to delete items you should just mark them. You can then recreate the array at a later point by copying only those items that are not marked for deletion.
Chopin2001 1-Jun-22 4:26am    
But wouldn't copy cost be an issue?
Richard MacCutchan 1-Jun-22 4:44am    
Possibly, but it all depends on context. All you have shown us is two arrays.
Chopin2001 1-Jun-22 7:23am    
sorry. This is variable and the size of the array members can also be longer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900