如何在javascript中实现字符串数组的映射

如何在javascript中实现字符串数组的映射,javascript,arrays,dictionary,Javascript,Arrays,Dictionary,我的代码: var appsAndScopes=[ ['1',['A','B','C','D']], ['2 ',['E','F','G','H']], ['3',['I']], ['4',['J']] ]; function getAllElements(id){return document.getElementById(id);} function buildDropdowns(){ var applications=getAllElements('application').

我的代码:

var appsAndScopes=[
['1',['A','B','C','D']],
['2 ',['E','F','G','H']],
['3',['I']],
['4',['J']]
];
function getAllElements(id){return document.getElementById(id);}
  function buildDropdowns(){
    var applications=getAllElements('application').options;  //Gets all applications names from appsAndScopes array.
    for(var applicationsIndex=0;applicationsIndex<appsAndScopes.length;applicationsIndex++){
      applications[applications.length]=new Option(appsAndScopes[applicationsIndex][0],appsAndScopes[applicationsIndex][0]);
    }

    getAllElements('application').onchange=function(){
      this.blur();
      var currentApplication=this.value;
      if(!currentApplication){return;}
      var scopes=getAllElements('scope').options;
      scopes.length=1;
      for(var scopesIndex=0;scopesIndex<appsAndScopes.length;scopesIndex++){
        if(appsAndScopes[scopesIndex][0]!==currentApplication){continue;}
        else{
          var temp=appsAndScopes[scopesIndex][1];
          for(var valueIndex=0;valueIndex<temp.length;valueIndex++){
            scopes[scopes.length]=new Option(temp[valueIndex],temp[valueIndex]);
          }
          break;
        }
      }
    }
  }
  $(function() {
      buildDropdowns();
  });   
var appsAndScopes=[
[1',[A','B','C','D'],
[2',[E','F','G','H'],
[3',[I']],
['4',['J']]
];
函数getAllegements(id){return document.getElementById(id);}
函数buildDropdowns(){
var applications=getAllegements('application')。options;//从appsAndScopes数组获取所有应用程序名称。
对于(var applicationsIndex=0;applicationsIndex声明映射对象

var map = new Map(); // or var map = {};
插入新项目

map.set(key, value);
在您的例子中,您可以使用like
map.set(1,['a','b','c','d');
检索项目

map.get(key); // will return value

有关更详细的用途,请参见此

您似乎不接受解决方案。当您接受解决方案时,您也会获得回购积分。不接受解决方案不会鼓励人们帮助您!