views:

8727

answers:

4

var myArray = new Object(); myArray["firstname"] = "Bob"; myArray["lastname"] = "Smith"; myArray["age"] = 25;

Now if I wanted to remove "lastname"?....is there some equivalent of myArray["lastname"].remove()?

(I need the element gone because the number of elements is important and I want to keep things clean).

Thanks in advance to everyone! Andrew

+27  A: 

Use the "delete" keyword in Javascript.

delete myArray["lastname"];
Dennis Cheung
+7  A: 

All objects in JavaScript are implemented as hashtables/associative arrays. So, the following are the equivalent:

alert(myObj["SomeProperty"]);
alert(myObj.SomeProperty);

And, as already indicated, you "remove" a property from an object via the delete keyword, which you can use in two ways:

delete myObj["SomeProperty"];
delete myObj.SomeProperty;

Hope the extra info helps...

Jason Bunting
Wow. That clears up a few misconceptions I had about js. Thx, Jason.
Mike
+2  A: 

awsome, thanks.

Don't post comments as answers. Use the "add comment" button.
Simon Howard
He can't post comments --- he doesn't have enough points.
Eugene Lazutkin
A: 

That only removes deletes the object but still keeps the array length same.

To remove you need to do something like:

array.splice(index, 1);

Bipin
Indeed, but in this case an array is not being used, just a plain old object, thus it has no length or splice method.
MooGoo