HighDots Forums  

javascript associative arrays

Javascript JavaScript language (comp.lang.javascript)


Discuss javascript associative arrays in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
jor
 
Posts: n/a

Default javascript associative arrays - 12-13-2007 , 09:49 PM






i want the key of a hash to be based on a variable:

i tried:

var c = 'blah';
var arr = { 'a':1000, b:2000, c.valueOf():3000 };


also
var arr = { 'a':1000, b:2000, c.toString():3000 };

why not?

it seems none of this is allowed.

is there a way to do this?

Reply With Quote
  #2  
Old   
Peter Michaux
 
Posts: n/a

Default Re: javascript associative arrays - 12-13-2007 , 09:59 PM






On Dec 13, 7:49 pm, jor <Johnston.Ro... (AT) gmail (DOT) com> wrote:
Quote:
i want the key of a hash to be based on a variable:

i tried:

var c = 'blah';
var arr = { 'a':1000, b:2000, c.valueOf():3000 };

also
var arr = { 'a':1000, b:2000, c.toString():3000 };

why not?
The PropertyName "keys" in an object literal can only be Identifier,
StringLiteral, or NumericLiteral. This is described in section 11.1.5
of the ECMAScript 3rd ed standard. Follow the link on the group FAQ to
get to the standard

<URL: http://jibbering.com/faq/#FAQ2_6>

Quote:
it seems none of this is allowed.

is there a way to do this?
var arr = {};
arr['a'] = 1000;
arr.b = 2000;
arr[c] = 3000;

Peter


Reply With Quote
  #3  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: javascript associative arrays - 12-14-2007 , 05:45 AM



Peter Michaux wrote:
Quote:
var arr = {};
arr['a'] = 1000;
arr.b = 2000;
arr[c] = 3000;
It should be pointed out (to the OP) that despite the chosen identifier
`arr', the referenced object is _not_ an "associative array" or a "hash" as
ECMAScript implementations at least until Edition 3 have no built-in concept
of this. `arr' refers to an Object object with user-defined properties.
`[...]' and `.' merely denote property accesses. `a', `b' and the value of
c are _not_ "keys", but they are property names.


PointedEars
--
"Use any version of Microsoft Frontpage to create your site. (This won't
prevent people from viewing your source, but no one will want to steal it.)"
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>


Reply With Quote
  #4  
Old   
VK
 
Posts: n/a

Default Re: javascript associative arrays - 12-14-2007 , 08:19 AM



On Dec 14, 6:59 am, Peter Michaux <petermich... (AT) gmail (DOT) com> wrote:
Quote:
is there a way to do this?

var arr = {};
arr['a'] = 1000;
arr.b = 2000;
arr[c] = 3000;
Or even
arr[f()] = 4000;
given that f is a function returning a valid PropertyName



Reply With Quote
  #5  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: javascript associative arrays - 12-14-2007 , 02:20 PM



VK wrote:
Quote:
On Dec 14, 6:59 am, Peter Michaux <petermich... (AT) gmail (DOT) com> wrote:
is there a way to do this?
var arr = {};
arr['a'] = 1000;
arr.b = 2000;
arr[c] = 3000;

Or even
arr[f()] = 4000;
given that f is a function returning a valid PropertyName
Regarding syntax, f() does _not_ need to return a valid PropertyName as it
already fulfills the condition for the bracket property accessor syntax of
being an Expression, specifically a CallExpression produced with a
MemberExpression followed by what can be produced with the Arguments production.

Regarding *functionality* though, it would have to return something that can
be produced with PropertyName for any reasonable property access because
there can only be property names that can be produced by that production.

However, as both the PropertyName production produces either an Identifier,
a StringLiteral or a NumericLiteral, and any return value is, if not already
a string, type-converted to String on property access, it can be anything
that the syntax for `return' allows, which is actually anything in the end.


PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.