How to delete arbitrary properties from an object, when they don'thave attribute syntax?

J

John Nagle

I'm using a Javascript object as an associative array to hold
a collection of domain names:

var domains = {}
var s = "example.com"
domains = true;

So far, so good. But how can I delete that entry by name?
The "." in the value creates problems:

delete domains.example.com;

means something else. And, of course, I need to specify
what to delete at run time.

John Nagle
 
J

John Nagle

Randy said:
John Nagle said the following on 2/6/2008 11:47 PM:
I'm using a Javascript object as an associative array to hold
a collection of domain names:

var domains = {}
var s = "example.com"
domains = true;

So far, so good. But how can I delete that entry by name?
The "." in the value creates problems:

delete domains.example.com;

means something else. And, of course, I need to specify
what to delete at run time.


delete domains['example.com']
delete domains

Bracket notation is your friend :)


OK, thanks. That makes sense, but isn't documented anywhere I've found.

John Nagle
 
J

Jeremy J Starcher

Randy said:
delete domains['example.com']
delete domains

Bracket notation is your friend :)


OK, thanks. That makes sense, but isn't documented anywhere I've
found.



My gut feeling said that was true, but I wanted to verify it before I
posted the answer. Randy beat me to it.

The only reference I could find comes from the ECMA-262, 11.2.1. Doesn't
mention the delete operator, but doesn't give any exceptions to the rule
that the two formats are identical.

MemberExpression . Identifier
is identical in its behaviour to
MemberExpression [ <identifier-string> ]
 
T

Thomas 'PointedEars' Lahn

There are no built-in associative arrays in ECMAScript implementations.
You are augmenting an object with properties.
a collection of domain names:

var domains = {}
var s = "example.com"
domains = true;

So far, so good. But how can I delete that entry by name?
The "." in the value creates problems:

delete domains.example.com;

means something else. And, of course, I need to specify
what to delete at run time.

delete domains['example.com']
delete domains


And so it is possible to remove those properties again.
OK, thanks. That makes sense, but isn't documented anywhere I've found.

Pardon? You have used the bracket property accessor just before.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Jeremy said:
Randy said:
delete domains['example.com']
delete domains

Bracket notation is your friend :)

OK, thanks. That makes sense, but isn't documented anywhere I've
found.


My gut feeling said that was true, but I wanted to verify it before I
posted the answer. Randy beat me to it.


Did he?
The only reference I could find comes from the ECMA-262, 11.2.1. Doesn't
mention the delete operator, but doesn't give any exceptions to the rule
that the two formats are identical.

Yes, it does give the exception: For the dot property accessor syntax, the
property name must be an *identifier*.
MemberExpression . Identifier
is identical in its behaviour to
MemberExpression [ <identifier-string> ]

Correct quotation, but irrelevant here. Anything that contains a dot is
_not_ an identifier. Relevant here is only:

| Properties are accessed by name, using either the dot notation:
|
| MemberExpression . Identifier
| CallExpression . Identifier
|
| or the bracket notation:
|
| MemberExpression [ Expression ]
| CallExpression [ Expression ]


PointedEars
 
J

John G Harris

There are no built-in associative arrays in ECMAScript implementations.
You are augmenting an object with properties.
<snip>

.... then using it to implement his associative array. And why not? There
are also no built-in linked lists or trees. Are you saying these data
structures can't be implemented in javascript?

John
 
J

Jeremy J Starcher

Jeremy J Starcher wrote:

Yes, it does give the exception: For the dot property accessor syntax,
the property name must be an *identifier*.

Must re-learn to be more technically correct in my speech.

Sometimes correct nomenclature really is important. I've gotten sloppy
lately.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,610
Members
45,255
Latest member
TopCryptoTwitterChannels

Latest Threads

Top