1

I have an object of items used as a dictionary with key/value pairs:

var stuff = {};
stuff["jar"] = "biscuit";

I'm trying to remove an item. So far, no array method seems to work. Is this even an array (as you probably guessed, I'm not too crash hot on Javascript).

How do I remove an item?

2 Answers 2

2

delete stuff['jar'] would remove it

you created an object literal and assigned the 'jar' property to be 'biscuit', [] is the syntax for a true array, though in ECMAScript arrays are really objects and should be used for numerically indexed sets as opposed to key/value specific sets

in short, you are using the right data type for the job.

Sign up to request clarification or add additional context in comments.

2 Comments

just out of curiosity -- I've seen this type of question before and the answer is always 'let the GC handle it'. Is this situation different?
@ajax81 - This is different. It's not about freeing up memory, it's about changing the state of the object by deleting one of its properties. In the simple example it was the only property, but if you imagine a more complicated object with lots of properties, if you deleted one of them and later enumerated the object's (remaining) properties in a for..in loop the deleted one would no longer be there - I'm sure you can imagine some reasons why you'd want to do that.
0

delete stuff['jar']

You have an object, not an array.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.