Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Nov 2003
    Location
    Northwest
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    removing elements from arrays/hashmaps

    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

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    Not entirely clear what the circumstances are here. Maybe this will help.

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Posts
    338
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Will this help (and is that what you mean)?

    Code:
    // 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!
    Last edited by nolachrymose; 12-18-2003 at 09:08 PM.

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts

    sample "hash array"

    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •