Forum Moderators: open

Message Too Old, No Replies

Remove Element From Object, not Array

.splice for Objects?

         

whoisgregg

9:16 pm on Sep 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello!

Trying to remove a particular element from an object. Sort of expected (well, hoped) this would work:

var theObject = new Object;
theObject = { "test_1" : "to", "test_2" : "the", "test_3" : "moon", "test_4" : "alice" };
theObject.splice('test_4',1);

But instead Firefox fails with a "theObject.splice is not a function." How does one delete an element from an Object?

penders

11:05 pm on Sep 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



How does one delete an element from an Object?

The clue is in the question.... the '

delete
' operator is used to delete properties from an object :) ...

delete theObject['test_4'];

or....
delete theObject.test_4;

But instead Firefox fails with a "theObject.splice is not a function."

.splice() won't work in IE either ("Object doesn't support this property or method") The splice() method only applies to nonassociative arrays. I think the reason being that the properties of an object are not guranteed to be in any particular order.

whoisgregg

2:26 pm on Sep 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Perfect! :D Thank you penders.

I did run across an interesting oddity. Using my simple example, it doesn't matter whether I use the dot notation or the bracket notation, it will delete both:

var theObject = new Object;
theObject = { "test_1" : "to", "test_2" : "the", "test_3" : "moon", "test_4" : "alice" };
delete theObject.test_4; // deletes both notations
//delete theObject['test_4']; // deletes both notations
document.write(theObject['test_4']+'<br>');
document.write(theObject.test_4+'<br>');

Code yields the same regardless of which delete line is used:

undefined
undefined

My true usage is for an OOP thingy (thingy is a technical term) and in the usage below, deleting the dot notation does not delete the bracket notation, but deleting the bracket notation deletes both notations. Here's that sample:

var theObject = new Object;
theObject = { "test_1" : "to", "test_2" : "the", "test_3" : "moon", "test_4" : "alice" };
function Cart(){
this.items = theObject;
this.remove = function(index){
delete this.items.index; // only deletes the dot notation
//delete this.items[index]; // deletes both notations
document.write(this.items.index+'<br>');
document.write(this.items[index]+'<br>');
}
}
cart = new Cart();
cart.remove('test_4');

Code as shown yields this output:

undefined
alice

Using the bracket notation though yields:

undefined
undefined

I'd love to learn why this works this way, although I am satisfied just using the bracket notation. :)

penders

4:25 pm on Sep 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



...Using my simple example, it doesn't matter whether I use the dot notation or the bracket notation, it will delete both

Yes, they mean exactly the same thing - both notations refer to the very same property. (There are not two different properties, one bracketed and one dotted.)

However, the bracket notation is absolutely necessary when you want to refer to a property value using another variable that contains the value of that property (in this respect it's very handy), as you are doing in your 2nd example, and I think is where you are getting a little confused - easy mistake though I think.

You cannot simply write:

delete theObject.myvariable;

(Which is the same as:)

delete theObject['myvariable'];
// Note the quotes

This is refering to an actual property called "myvariable" (not the property name which is contained within the variable 'myvariable') - and there isn't one (unless you had defined one specifically!).

In your 2nd example:

delete this.items.index; // only deletes the dot notation

This doesn't actually do anything at all! You are trying to delete a property that is called 'index', and there isn't one! index is the name of your variable, not the name of the property (you must use the bracket notation in this case). You use it correctly in your bracket notation - without the quotes. If you had put quotes in (ie. ['index']) then this would have meant the same as your dot notation (.index) and would not have worked.

Likewise:

document.write(this.items.index+'<br>');

This will always yield 'undefined' (try doing this before issueing the delete statement), because there is no property called 'index'.

Hope that clears things up - looks like you already have the crunch of it anyway.

One last example... these two delete statements are equivalent:

var myvar = 'propertyname'; 
delete myObject.propertyname; // SAME
delete myObject[myvar]; // SAME

To be honest, I've mostly used the bracket notation, since my property names have often been generated at runtime and held in variables. It is more flexible in this respect.

whoisgregg

8:12 pm on Sep 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I thought I had the crunch of it, but I had been confused. I truly appreciate your post that lays it out. I've got it now, and understand my mistake. Thank you. :D