473,218 Members | 1,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,218 software developers and data experts.

Associative array key

I need help, im sure the solution is simple, but it's driving me nuts. Im trying to retrieve the value of js_array['pearl']['a'] using js_array['pearl'][selectedObj.selectedIndex]; where selectedObj.selectedIndex is a string value from a select box. I need to wrap quotes around selectedObj.selectedIndex for it to retrieve the associative key properly, but everything ive tried has failed. This works fine if the key is numeric, but fails if the key is a string. I'm sorry if I didn't explain this very well, my brain is kind of fried today. Can someone please help me???

Expand|Select|Wrap|Line Numbers
  2. <script language='JavaScript'> 
  3. var js_array = new Array();
  4. js_array['stain'] = new Array();
  7. js_array["pearl"]["a"] = "pearla.jpg";
  10. function pearlchange(selectedObj){
  11. document.pearlimg.src=imgDir + js_array['pearl'][selectedObj.selectedIndex];
  12. }
  13. </script>
  15. <form>
  16. <select name="pearl" onchange='pearlchange(this)' >
  17. <option value='a'>Violet (a)</option>
  19. </select>
  20. </form>
  23. <img name='pearlimg' src="" />
Jan 30 '09 #1
7 2493
16,027 Expert Mod 8TB
Use selectedObj.value in place of selectedIndex.
Jan 30 '09 #2
5,390 Expert Mod 4TB
just a note:

JavaScript has no associative Arrays ... even when you do what you did. In fact you extend the native JavaScript array with new properties ... and this is a 'misuse' of the array as an object ... so the correct way would be to use an object at all. the misuse could lead to strange behaviour when you use for-in-loops or rely on array-methods or properties ... for example length will be zero.

so the correct way would be ... the alert shows the construct in FF:

Expand|Select|Wrap|Line Numbers
  1. var foo = {};
  2. var bar = {};
  4. bar.key1 = 'foobar';
  6. foo.foo_key1 = bar;
  8. alert(foo.toSource());
kind regards
Jan 31 '09 #3
Thanks for the help, much appreciated.

Feb 2 '09 #4
I have another problem now. FF doesn't like it when I assign a numeric "key" to my object. What am I doing wrong here?

Expand|Select|Wrap|Line Numbers
  1. var stain = {};
  2. var pearl = {};
  3. stain.1 = 'stain1.jpg';  //This produces a 'missing ; before statement' error
  4. pearl.a = 'pearla.jpg'; //This works fine

I'm also having problem with using a variable as my object "key" value:

Expand|Select|Wrap|Line Numbers
  2. var optionImg = {};
  3. var pearl = {};
  4. pearl.a = 'pearla.jpg';
  5. pearl.b = 'pearlb.jpg';
  6. pearl.c = 'pearlc.jpg';
  7. pearl.e = 'pearle.jpg';
  8. pearl.f = 'pearlf.jpg';
  9. pearl.g = 'pearlg.jpg';
  10. pearl.h = 'pearlh.jpg';
  11. optionImg.pearl = pearl;
  12. alert(optionImg.toSource());
  13. var imgDir='http://vikingcue.com/images/options/';
  16. function pearlChange(selectedObj){
  17. var testpath=imgDir + optionImg.pearl.selectedObj;
  18. alert(selectedObj in optionImg.pearl.toSource());
  19. alert(testpath.toSource());
  20. }
I need the selectObj variable to reference the key, instead I get the error: optionImg.pearl.selectedObj is undefined. I'm sorry if these questions are elementary, I'm just trying to learn a few things here.

Here is the select box:

<select onChange='pearlChange(this.value)>
<option value='a'>Pearl A</option>


Feb 3 '09 #5
16,027 Expert Mod 8TB
Use square bracket notation:
Expand|Select|Wrap|Line Numbers
  1. optionImg.pearl[selectedObj]
Feb 3 '09 #6
Thanks, that fixed both problems I was having.
Feb 3 '09 #7
16,027 Expert Mod 8TB
You're welcome. Glad it did!
Feb 3 '09 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

by: Stefan Richter | last post by:
Hi, I want to create an associative Array with a PHP variable (article ID) as Key and another associative array as it's value. How do I instanciate it, how can I fill it? I want something...
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";
by: mark4asp | last post by:
Suppose I have the following code. It functions to randomly select a city based upon the probabilities given by the key differences in the associative array. . Eg. because the key difference...
by: Robert | last post by:
I am curious why some people feel that Javascript doesn't have associative arrays. I got these definitions of associative arrays via goggle: Arrays in which the indices may be numbers or...
by: Derek Basch | last post by:
Is there any way to associate name/value pairs during an array initialization? Like so: sType = "funFilter" filterTypeInfo = ; filterTypeInfo = new Array("type" : sType); I can do it using...
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they...
by: soup_or_power | last post by:
Hi I have an associative array like this: arr=30; arr=20;arr=40;arr=10; I want the sort function to sort keys in ascending order of the values on the right hand side with the following result:...
by: Robert Mark Bram | last post by:
Hi All! How do you get the length of an associative array? var my_cars= new Array() my_cars="Mustang"; my_cars="Station Wagon"; my_cars="SUV"; alert(my_cars.length);
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...
by: Bosconian | last post by:
I'm trying to output the contents of an array of associative arrays in JavaScript. I'm looking for an equivalent of foreach in PHP. Example: var games = new Array(); var teams = new...
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.