Re: Select 1 value from multiple...
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.javascript archive

Re: Select 1 value from multiple...

From: Dr John Stockton <jrs@merlyn.demon.co.uk>
Date: Thu Aug 11 2005 - 16:27:41 CEST

JRS: In article <zcjKe.172865$s54.111428@pd7tw2no>, dated Wed, 10 Aug
2005 08:49:35, seen in news:comp.lang.javascript, Noozer
<dont.spam@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.
Received on Tue Oct 18 03:07:39 2005