473,320 Members | 1,957 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,320 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
  1.  
  2. <script language='JavaScript'> 
  3. var js_array = new Array();
  4. js_array['stain'] = new Array();
  5.  
  6.  
  7. js_array["pearl"]["a"] = "pearla.jpg";
  8.  
  9.  
  10. function pearlchange(selectedObj){
  11. document.pearlimg.src=imgDir + js_array['pearl'][selectedObj.selectedIndex];
  12. }
  13. </script>
  14.  
  15. <form>
  16. <select name="pearl" onchange='pearlchange(this)' >
  17. <option value='a'>Violet (a)</option>
  18.  
  19. </select>
  20. </form>
  21.  
  22.  
  23. <img name='pearlimg' src="" />
  24.  
Jan 30 '09 #1
7 2497
acoder
16,027 Expert Mod 8TB
Use selectedObj.value in place of selectedIndex.
Jan 30 '09 #2
gits
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 = {};
  3.  
  4. bar.key1 = 'foobar';
  5.  
  6. foo.foo_key1 = bar;
  7.  
  8. alert(foo.toSource());
kind regards
Jan 31 '09 #3
Thanks for the help, much appreciated.

Justin
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
  5.  

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

Expand|Select|Wrap|Line Numbers
  1.  
  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/';
  14.  
  15.  
  16. function pearlChange(selectedObj){
  17. var testpath=imgDir + optionImg.pearl.selectedObj;
  18. alert(selectedObj in optionImg.pearl.toSource());
  19. alert(testpath.toSource());
  20. }
  21.  
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>
</select>

Thanks,

Justin
Feb 3 '09 #5
acoder
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
acoder
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

11
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...
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: 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...
4
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...
8
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...
47
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...
5
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:...
7
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);
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...
11
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.