我从页面上拿到a,200  b,300   c,400   a,100   这样四组信息。我怎么建一个数组 放 他们,然后循环  得到新数组 a,300  b,300 c,400  

解决方案 »

  1.   

    楼主可以考虑用下hash表 ,给你个例子参考下,hash表类网上的,修改了下;
    <script type="text/javascript">
    function Hashtable() {  
    this._hashValue= new Object();  
    this._iCount= 0;  
    }  Hashtable.prototype.add = function(strKey, value) {  
    if(typeof(strKey) == "string"){  
    if(this.contain(strKey)){//这段修改过,为了满足你的需求
    this._hashValue[strKey]= typeof(value) != "undefined"? value+this._hashValue[strKey] : null;  
    }else
    {
    this._hashValue[strKey]= typeof(value) != "undefined"? value: null; 
    this._iCount++;  
    }
     return true;  
    }  
    else  
    throw "hash key not allow null!";  
    }  Hashtable.prototype.get = function(key) {  
    if (typeof(key)== "string" && this._hashValue[key] != typeof('undefined')) {  
    return this._hashValue[key];  
    }  
    if(typeof(key) == "number")  
    return this._getCellByIndex(key);  
    else  
    throw "hash value not allow null!";   return null;  
    }  Hashtable.prototype.contain = function(key) {  
    return this.get(key) != null;  
    }  Hashtable.prototype.findKey = function(iIndex) {  
    if(typeof(iIndex) == "number")  
    return this._getCellByIndex(iIndex, false);  
    else  
    throw "find key parameter must be a number!";  
    }  Hashtable.prototype.count = function () {  
    return this._iCount;  

     
    Hashtable.prototype._getCellByIndex = function(iIndex, bIsGetValue) {  
    var i = 0;  
    if(bIsGetValue == null) bIsGetValue = true;  
    for(var key in this._hashValue) {  
    if(i == iIndex) {  
    return bIsGetValue ? this._hashValue[key] : key;  
    }  
    i++;  
    }  
    return null;  
    }  Hashtable.prototype.remove = function(key) {  
    for(var strKey in this._hashValue) {  
    if(key == strKey) 
    {  
    deletethis._hashValue[key];  
    this._iCount--;  
    }  
    }  
    }  Hashtable.prototype.clear = function () {  
    for (var key in this._hashValue) {  
    delete this._hashValue[key];  
    }  
    this._iCount = 0;  
    }  
    </script>
    <script type="text/javascript">
    var hash=new Hashtable();//简单写了下
    hash.add("a",1);
    hash.add("b",3);
    hash.add("c",13);
    hash.add("c",6);
    hash.add("a",7);
    var v=[];
    for(var i=0;i<hash.count();i++){
    v.push(hash.findKey(i)+","+hash._getCellByIndex(i));
    }
    alert(v.join(" "));
    </script>
      

  2.   

    function Objs(j, y, b) {
      this['甲'] = j;
      this['乙'] = y;
      this['丙'] = b;
    }var objs1 = new Objs(100, 200, 300);var objs2 = new Objs(200, 200, 300);for(var i in objs2) {
      alert(i + ':' + objs2[i]);
    }
      

  3.   


    <script type="text/javascript">
    var suy = "a:200,b:300,c:400,a:100";
    var arr = suy.split(",");
    var obj = {};
    for(var i=0, l = arr.length; i < l; ++i)
    {
    var h = arr[i].split(":");
    obj[h[0]] = (obj[h[0]]||0) + h[1]*1;
    }
    for (var i in obj)
    {
    document.write(i,":",obj[i]," ");
    }
    </script>
      

  4.   

    额 错了...貌似是得用双重循环var arr=[{key:"a",val:[1,2,3...200]},{key:"b",val:[1,2,3....100]},{key:"c",val:[1,2,200]},{key:"a",val:[1,2,..100]}];
    for(var i=1;i<arr.length;i++){
    var biaoj=false;
    for(var j=0;j<i;j++){
    if(arr[j].key==arr[i].key){
    arr[j].val==arr[j].concat(arr[i].val);
    arr.splice(i,1);
    }}}大概这个意思吧...有点不好想,另外 splice貌似低级别ie不支持