views:

1071

answers:

5

Given

var myHash = new Array();
myHash['key1'] = { Name: 'Object 1' };
myHash['key2'] = { Name: 'Object 2' };
myHash['key3'] = { Name: 'Object 3' };

how do I remove key2, and object 2 from the hash, that it ends up in a state as if i did:

var myHash = new Array();
myHash['key1'] = { Name: 'Object 1' };
myHash['key3'] = { Name: 'Object 3' };

delete doesnt do what i want;

delete myHash['key2']

simply gives me this:

var myHash = new Array();
myHash['key1'] = { Name: 'Object 1' };
myhash['key2'] = null;
myHash['key3'] = { Name: 'Object 3' };

the only docs i can find on splice and slice deal with integer indexers, which i dont have.

Edit: I also do not know that 'key2' is necessarily in position [1]

UPDATE

OK slight red herring, delete does seem to do what i want on the surface, however, im using json2.js to stringify my object to json for pushing back to the server,

after ive deleted, myHash gets serialised as:

[ { Name: 'Object 1' }, null, { Name: 'Object 3' } ]

Is this a bug in json2.js? or is it something im doing wrong with delete?

Thanks

+5  A: 

You're looking for delete:

delete myhash['key2'];

See the Core Javascript Guide

karim79
I thought this was correct, thats why i tried it in the first place. It does _seem_ to work, my problem actually lies in Json.stringify, ill update the question
Andrew Bullock
use the splice function instead. Delete leaves a hole in the array.
David Andres
This work for me. Thanks!
x13
+1  A: 

Another option may be this John Resig remove method. can better fit what you need. if you know the index in the array.

andres descalzo
well not having the index is exactly what the question states
Andrew Bullock
+2  A: 

Why do you use new Array(); for hash? You need to use new Object() instead.

And i think you will get what you want.

Eldar Djafarov
I don't like new at all. I prefer var myHash = [];
Nosredna
Or in this case, myHash={}; :-)
Nosredna
@Nosredna totally agree:)
Eldar Djafarov
I agree.The only thing that an array gives you is a length which doesn't ever work when you change the indexer something other than an int.
x13
+3  A: 

You say you don't necessarily know that 'key2' is in position [1]. Well, it's not. Position 1 would be occupied by myHash[1].

You're abusing JavaScript arrays, which (like functions) allow key/value hashes. Even though JavaScript allows it, it does not give you facilities to deal with it, as a language designed for associative arrays would. JavaScript's array methods work with the numbered properties only.

The first thing you should do is switch to objects rather than arrays. You don't have a good reason to use an array here rather than an object, so don't do it. If you want to use an array, just number the elements and give up on the idea of hashes. The intent of an array is to hold information which can be indexed into numerically.

You can, of course, put a hash (object) into an array if you like.

myhash[1]={"key1","brightOrangeMonkey"};
Nosredna
you are correct, thanks :) see my answer
Andrew Bullock
+1  A: 

Thanks everyone for your tellings about Object vs Array

In my code i have an array and hash of the same data and i'm confusing them, leading me to this question based on my confused understanding of the problem. doh!

Deleting from my object works properly, deleting from the array leaves a hole.

sorry for the confusion, got there in the end!

@JonSkeet, it wasnt an array, hence the missing method!

Andrew Bullock