标签云

微信群

扫码加入我们

WeChat QR Code

如何解决

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 }]


That's because array is not an array.

2018年05月28日20分07秒

Jan Dvorak - I know that is the problem (thus the question title...), but what I have to do to accomplish that I am looking for?

2018年05月27日20分07秒

var array = new Array(); ??

2018年05月27日20分07秒

var array = {} is an object not array. You can't use array function on the objects.

2018年05月27日20分07秒

Billy: Deleted my comment but anyway: Arrays are just objects, so each element of an array is actually a property with a numeric key.

2018年05月27日20分07秒

there's more to it. He's trying to use an Object as a key.

2018年05月27日20分07秒

Jan Dvorak - It's true.

2018年05月27日20分07秒

user12882: You cannot use objects as keys. Anything you use as key will be converted to a string. The default string representation for objects is "[object Object]". You could overwrite the toString method of each object to return a unique identifier but this solution is not very common (afaik) and might be more confusing than helpful. Better learn first how objects work: developer.mozilla.org/en-US/docs/JavaScript/Guide/….

2018年05月27日20分07秒

JanDvorak I don't know who down-voted you, I sorry you were down-voted, but it has nothing to do with me I am afraid.

2018年05月27日20分07秒

Did you try to run the fiddle? It actually shows that array[an_object] is overwritten by array[another_object]

2018年05月27日20分07秒

Yes I did run the fiddle

2018年05月27日20分07秒