<br><br>a1=['020','021',..........'025',..........]; <br>a2=['上海','天津',..........'南京',..........]; <br>length一样长,并且一一对应,怎么得到a1里面025对应的a2的南京? <br><br>自己想了个办法,但是用到了很多eval还有循环,感觉不是很好: <br><div class="htmlarea"> <textarea id="runcode98054"> <script> var a1=['020','021','025'],a2=['上海','天津','南京']; var obj=function(){ for(var i=0;i<a1.length;i++){ eval("this.a"+a1[i]+"='"+a2[i]+"';") } this.getA=function(m){ return (eval("this.a"+m)) } } var obj2=new obj; alert(obj2.getA('025')) </script> </textarea><br><input onclick="runEx('runcode98054')" type="button" value="运行代码"> <input onclick="doCopy('runcode98054')" type="button" value="复制代码"> <input onclick="doSave(runcode98054)" type="button" value="保存代码">[Ctrl+A 全选 注:<a href="http://www.jb51.net/article/23421.htm" title="查看具体详情" target="_blank">如需引入外部Js需刷新才能执行</a>]</div> <br><br>只要你确定了编号位置为三位,, <br>然后就ok! <br><div class="htmlarea"> <textarea id="runcode89768"> <script> var a1=['020','021','025','999'],a2=['上海','天津','南京','xpxp']; alert(a2[(a1.join().indexOf('021'))/4]); </script> </textarea><br><input onclick="runEx('runcode89768')" type="button" value="运行代码"> <input onclick="doCopy('runcode89768')" type="button" value="复制代码"> <input onclick="doSave(runcode89768)" type="button" value="保存代码">[Ctrl+A 全选 注:<a href="http://www.jb51.net/article/23421.htm" title="查看具体详情" target="_blank">如需引入外部Js需刷新才能执行</a>]</div> <br>这个是数组的实现方法 <br><div class="htmlarea"> <textarea id="runcode67741"> <script> var a1=['020','021','025'],a2=['上海','天津','南京']; function getCity(n){ for(var i=0, l=a1.length; i<l; i++) if(n==a1[i])return a2[i]; } alert(getCity("025")) </script> </textarea><br><input onclick="runEx('runcode67741')" type="button" value="运行代码"> <input onclick="doCopy('runcode67741')" type="button" value="复制代码"> <input onclick="doSave(runcode67741)" type="button" value="保存代码">[Ctrl+A 全选 注:<a href="http://www.jb51.net/article/23421.htm" title="查看具体详情" target="_blank">如需引入外部Js需刷新才能执行</a>]</div> <br><br>高手支招 <br>如果我完成类似功能,我不会选择数组; <br><div class="htmlarea"> <textarea id="runcode7262"> <script type="text/javascript"> var pros = { '021' : '上海', 'o22' : '北京', 'o23' : '深圳' } alert(pros['021']) </script> </textarea><br><input onclick="runEx('runcode7262')" type="button" value="运行代码"> <input onclick="doCopy('runcode7262')" type="button" value="复制代码"> <input onclick="doSave(runcode7262)" type="button" value="保存代码">[Ctrl+A 全选 注:<a href="http://www.jb51.net/article/23421.htm" title="查看具体详情" target="_blank">如需引入外部Js需刷新才能执行</a>]</div> <br><br><br><br>