The Little Guy Posted March 7, 2008 Share Posted March 7, 2008 I have an array that could look simular to this: playersArr = Array(1 => 'cat', 3 => 'fish', 9 => 'bird', 5 => 'mouse'); function removePlayer(id){ playersArr.splice(id,1); } Then there is a function, that is supposed to remove an element from the array (removePlayer). I then have a button next to each one to remove it from the array. So if you want to remove one you press the button and it fires up removePlayer() and removes that id from the array. The problem is that if I remove from the array 1, 3, 9, 5 in that order (one at a time) it will remove all but #5. But... If I remove from the array 5, 9, 3, 1 in that order (one at a time) it will remove them all. does that make any sense??? Quote Link to comment Share on other sites More sharing options...
fenway Posted March 7, 2008 Share Posted March 7, 2008 What's wrong with "delete"? Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted March 7, 2008 Author Share Posted March 7, 2008 Thank You! Never though of that... Not even sure I knew there was a delete Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted March 7, 2008 Author Share Posted March 7, 2008 If I have 1 key value pair, and I delete it. why does it still think it is in there? if I alert(playersArr.length); it says 2 (1 key 1 value = 2) Quote Link to comment Share on other sites More sharing options...
fenway Posted March 7, 2008 Share Posted March 7, 2008 That's quirky because you're using integers as hash keys... associative array in JS are funny. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted March 9, 2008 Author Share Posted March 9, 2008 Any Ideas? Quote Link to comment Share on other sites More sharing options...
fenway Posted March 10, 2008 Share Posted March 10, 2008 Sure... don't use integers, or prefix them with underscores. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.