View Full Version : removing elements from arrays/hashmaps

12-18-2003, 07:03 PM
hi everybody.

was wondering if any of you fine genius-minded folks could give me a clue on removing elements from arrays and tables...without a key index...the scenario is a text-based adventure game. I have created hashmaps to hold items in a room and also one to hold the items a player possesses. problem is I cant find anything at the documentation page, that might make this possible.

The remove() method needs an index key, which was my original plan, however in that aspect i need to know how to find out the key of the entry.

Any suggestions for either situation would help me to progress to the final stage of this demanding assignment.

Thanking you all muchly

12-18-2003, 07:36 PM
Not entirely clear what the circumstances are here. Maybe this (http://safari.oreilly.com/?x=1&mode=section&sortKey=title&sortOrder=asc&view=&xmlid=0-596-00467-2/jvdhtmlckbk-CHP-3-SECT-10&open=true&g=&catid=&s=1&b=1&f=1&t=1&c=1&u=1&r=&o=1) will help.

12-18-2003, 10:06 PM
Will this help (and is that what you mean)?

// returns true if element was found and deleted, otherwise returns false;
function removeElement(descStr) {

for(var i in this) {

if(this[i] == descStr) {

delete this[i];
return true;



return false;


Array.prototype.removeElement = removeElement;

Hope that helps!

Happy coding! :)

12-19-2003, 03:55 AM
var arr = new Array();
arr["anykey"] = "blah";
arr["anotherkey"] = "blah blah";

then to delete:
delete arr["key"];

take note that the delete operator does not actually delete the element but just set it to undefined. You cannot use Array.slice() because it requires a zero-based index as parameters.