&b=2&b=3
我想生成一个对象temp 可以用  temp['b'] =2 这样引用。

解决方案 »

  1.   

    &b=2&b=3,请问temp["b"]=2的话另外一个3怎么处理呢?使用函数取吧,然后typeof()判断为string则直接使用,object说明是数组对象。
    var getParam = function (key) {
    var arr = window.location.search.slice(1).replace(/\+/g, ' ').split('&');
    var result = undefined;
    for (var i = 0; i < arr.length; i++) {
    if (arr[i].split('=')[0] === key) {
    if (!result) {
    result = arr[i].split('=')[1];
    } else {
    result = [result].concat(arr[i].split('=')[1]);
    }
    }
    }
    return result;
    }
      

  2.   


    这个逻辑写的不对,不好意思。
    result = (typeof (result) === 'string' ? [result] : result).concat(arr[i].split('=')[1]);
    或者直接声明result=[],然后push新元素进去,return时判断下再返回。