Possible Duplicate:
How to remove a property from a javascript object
JavaScript Hashmap Equivalent
I am using jQuery and I am handling a variable this way:
var array = {};
array[an_object] = something
array[another_object] = something_else
array[...] = ...
When I try to run the splice method on the array I get a TypeError: array.splice is not a function. My intent is to remove the an_object “key” and all its content from the array variable.
How can I make that?
Note: When I run the console.log(array[an_object]) (the same is valid for another_object and all other objects) I get:
[Object { label="str1", value=1 }, Object { label="str2", value=2 }, { label="strN", value=N }]
First of all, name your variables what they are. The name
arrayyou’re using, is misleading if you use it to create a object.Now, you can delete the entry in the object with the
deletestatement:Now, that said, this will not work like you’d expect.
myObject[an_object]will contain “abc”Don’t use objects as keys. Use strings, instead.
This is because of the fact that any parameter entered in the
[]will be converted to string. So actually, you’re enteringmyObject["[object Object]"]