473,467 Members | 1,596 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

javascript associative arrays

jor
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?
Dec 14 '07 #1
3 1125
On Dec 13, 7:49 pm, jor <Johnston.Ro...@gmail.comwrote:
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>
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
Dec 14 '07 #2
Peter Michaux wrote:
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>
Dec 14 '07 #3
VK
On Dec 14, 6:59 am, Peter Michaux <petermich...@gmail.comwrote:
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

Dec 14 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Kevin | last post by:
Help! Why are none of these valid? var arrayName = new Array(); arrayName = new Array('alpha_val', 1); arrayName = ; I'm creating/writing the array on the server side from Perl, but I
27
by: Abdullah Kauchali | last post by:
Hi folks, Can one rely on the order of keys inserted into an associative Javascript array? For example: var o = new Object(); o = "Adam"; o = "Eve";
6
by: Chris Styles | last post by:
Dear All, I've been using some code to verify form data quite happily, but i've recently changed the way my form is structured, and I can't get it to work now. Originally : The form is...
4
by: meltedown | last post by:
Theres something very basic about javascript arrays I'm missing. The value of unit.value is 17.00 and value of the qty.value is 5 and I put these values into an array: myarray.value]=qty.value;...
104
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
41
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.