469,271 Members | 997 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

Select 1 value from multiple...

I want to return one of several values, based on the value of a variable.

Is there a compact way to write this? Something like:

var J=5;
var K= {"One","Two","Three","Four","Five","Six"} [J] ;
.... so K would hold "Five" in this case.

Thx
Aug 10 '05 #1
4 1217

"Noozer" <do*******@me.here> wrote in message
news:zcjKe.172865$s54.111428@pd7tw2no...
I want to return one of several values, based on the value of a variable.

Is there a compact way to write this? Something like:

var J=5;
var K= {"One","Two","Three","Four","Five","Six"} [J] ;
... so K would hold "Five" in this case.

Thx
Use an array.
var K=new Array("One","Two","Three","Four","Five","Six");
var J=4;
document.write(K[J]);
would produce 'five' remember that arrays start with a zero offset

Best wishes

Darren

Aug 10 '05 #2
Noozer wrote:
I want to return one of several values, based on the value of a variable.

Is there a compact way to write this? Something like:

var J=5;
var K= {"One","Two","Three","Four","Five","Six"} [J] ;
... so K would hold "Five" in this case.


var J=5;
var K= (new Array("One","Two","Three","Four","Five","Six"))[J-1];

--
Klaus Johannes Rusch
Kl********@atmedia.net
http://www.atmedia.net/KlausRusch/
Aug 10 '05 #3

"Klaus Johannes Rusch" <Kl********@atmedia.net> wrote in message
news:3l*************@individual.net...
Noozer wrote:
I want to return one of several values, based on the value of a variable.
Is there a compact way to write this? Something like:

var J=5;
var K= {"One","Two","Three","Four","Five","Six"} [J] ;
... so K would hold "Five" in this case.


var J=5;
var K= (new Array("One","Two","Three","Four","Five","Six"))[J-1];


Just wondering if there was a way to combine it on one line... No problem.
This works.
Aug 10 '05 #4
JRS: In article <zcjKe.172865$s54.111428@pd7tw2no>, dated Wed, 10 Aug
2005 08:49:35, seen in news:comp.lang.javascript, Noozer
<do*******@me.here> posted :
I want to return one of several values, based on the value of a variable.

Is there a compact way to write this? Something like:

var J=5;
var K= {"One","Two","Three","Four","Five","Six"} [J] ;
... so K would hold "Five" in this case.


var J=5;
var K= [,"One","Two","Three","Four","Five","Six"][J] ;

Changed {} to [] and insert comma.

Actually, K holds a reference to "Five"; in this case, that does not
matter, but in others it might.

Note that (AIUI) the array should be created whenever used; the
following would be more efficient in that case.

var Numbers = [,"One","Two","Three","Four","Five","Six"] ; // Global
....
var J = 5 ;
var K = Numbers[J] ;

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Aug 11 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Rolf Wester | last post: by
7 posts views Thread by Felix Natter | last post: by
6 posts views Thread by Ben Hallert | last post: by
5 posts views Thread by callmebill | last post: by
2 posts views Thread by areef.islam | last post: by
2 posts views Thread by murraymiken | last post: by
1 post views Thread by abhishekhs | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.