Home  >  Article  >  Web Front-end  >  js实现的类似于asp数据字典的数据类型代码实例_javascript技巧

js实现的类似于asp数据字典的数据类型代码实例_javascript技巧

WBOY
WBOYOriginal
2016-05-16 16:37:391319browse

首先声明一个数组:

复制代码 代码如下:

var dictNew=new Array;

 var key;

var value;

 for (var i = 0; i

  //获取要添加到数据字典的键值对
    key= jQuery("#costCodeIdId"+i).val();

  value = num2zero(jQuery("#valueId"+i).val());

  //检查该key值是否在数据字典中存在,如果不存在,直接把key值和value添加到数据字典中,如果存在该key键值,则value值累加

    if(checkHasInDict(key,dictNew)){
       dictNew[key] = num2zero(dictNew[key]) + value;
    }else{
       dictNew[key] = value;
    }

}

 

//数据字典的取值

function getDictValue(key,dict){

  var tempDictValue = "";

  for(var k in dict){

    if(k==key){

      tempDictValue =dict[k];

      return tempDictValue;

    }

  }

  return tempDictValue;

}

 

//检查是key值是否在数组中存在
function checkHasInDict(key,dict){
 for(var k in dict){
  if (k == key){
   return true;
  }
 }
 return false ;
}

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