Home >Web Front-end >JS Tutorial >Various ways to solve JavaScript array problems_javascript skills

Various ways to solve JavaScript array problems_javascript skills

WBOY
WBOYOriginal
2016-05-16 19:02:441319browse


a1=['020','021',..........'025',..........];
a2=[' Shanghai','Tianjin',.........'Nanjing',..........];
length is the same length and corresponds one to one. How to get 025 in a1 Corresponding to Nanjing of a2?

I thought of a way, but it used a lot of evals and loops, which didn’t feel very good:

[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute
]


As long as you are sure The numbering position is three digits,
and then it’s ok!

[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute
]
This is the implementation method of array
[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute
]

Advice from experts

[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute
]
<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><script> var a1=['020','021','025','999'],a2=['上海','天津','南京','xpxp']; alert(a2[(a1.join().indexOf('021'))/4]); </script><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>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn