Javascript 尝试在jquery中组合两个数组时出错

Javascript 尝试在jquery中组合两个数组时出错,javascript,jquery,html,cross-browser,Javascript,Jquery,Html,Cross Browser,我使用这个函数来组合两个长度相等的数组的结果 例如:如果am组合两个数组,则表示,数组A和数组B combined = fields.reduce(function(obj, val, i) { obj[val] = edit_opt[i]; return obj; }, {}); 输出的格式为array[数组A的值]=数组B的值 combined = fields.reduce(function(obj, val, i) { obj[val] = edit_opt[i

我使用这个函数来组合两个长度相等的数组的结果

例如:如果am组合两个数组,则表示,
数组A
数组B

combined = fields.reduce(function(obj, val, i) {
    obj[val] = edit_opt[i];
    return obj;
}, {});
输出的格式为
array[数组A的值]=数组B的值

combined = fields.reduce(function(obj, val, i) {
    obj[val] = edit_opt[i];
    return obj;
}, {});
当在chrome和firefox中测试时,这个函数实现了我想要的功能,但是当我在IE 8,9中测试代码时,我得到了一个错误。 我已经在下面发布了消息

网页错误详细信息

如何解决这个错误

Array.prototype.reduce是对ECMAScript 5的添加;因此,它可能不会出现在标准的其他实现中

您可以通过在脚本开头插入以下代码来解决这个问题,允许在本机不支持reduce的实现中使用reduce

if (!Array.prototype.reduce) {  
        Array.prototype.reduce = function reduce(accumulator){  
        if (this===null || this===undefined) throw new TypeError("Object is null or undefined");  
        var i = 0, l = this.length >> 0, curr;  

        if(typeof accumulator !== "function") // ES5 : "If IsCallable(callbackfn) is false, throw a TypeError exception."  
          throw new TypeError("First argument is not callable");  

        if(arguments.length < 2) {  
          if (l === 0) throw new TypeError("Array length is 0 and no second argument");  
          curr = this[0];  
          i = 1; // start accumulating at the second element  
        }  
        else  
          curr = arguments[1];  

        while (i < l) {  
          if(i in this) curr = accumulator.call(undefined, curr, this[i], i, this);  
          ++i;  
        }  

        return curr;  
      };    
 }  
if(!Array.prototype.reduce){
Array.prototype.reduce=函数reduce(累加器){
如果(this==null | | this==undefined)抛出新的TypeError(“对象为null或未定义”);
变量i=0,l=this.length>>0,curr;
if(typeof acculator!=“function”)//ES5:“如果IsCallable(callbackfn)为false,则引发TypeError异常。”
抛出新类型错误(“第一个参数不可调用”);
如果(arguments.length<2){
如果(l==0)抛出新的TypeError(“数组长度为0,没有第二个参数”);
curr=this[0];
i=1;//从第二个元素开始累加
}  
其他的
curr=参数[1];
而(i

尝试添加“var combined=…”而不是“combined=…”
reduce
函数在IE 8中也不受支持。所以你可以省去这一点。没有实现。试着运行
[]。在那些浏览器中减少
,看看有什么不同。@Engineer为我工作得很好,非常感谢你,伙计,我整个上午都在绞尽脑汁,感谢你andreas,davin和amberlamps帮了我一把。@user1542535欢迎这样做,如果答案对你有效,那么也许你能做到。