如何在IE 11中合并对象 [英] How to merge object in IE 11

查看:262
本文介绍了如何在IE 11中合并对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下对象数组:

Objs[0] = {Name : "ABC"};
Objs[1] = {Roll : 123}

我正在尝试制作它如下所示:

I'm trying to make it as the following:

Objs {
  Name : "ABC",
  Roll : 123
}

我尝试使用以下代码制作它:

I attempted to make it with the following code:

var Objs = [{
  Name: "ABC"
}, {
  Roll: 123
}];

console.log(
  Object.assign.apply(null, [{}].concat(Objs)) // 1
)
or 

console.log(
  Object.assign({}, ...Objs) // 2
)

问题是这在IE 11中不起作用。

The problem is that this is not working in IE 11.

我收到错误:


错误1:无法获取未定义或空引用的属性

Error for 1 : Unable to get property 'on' of undefined or null reference

2的错误:对象不支持属性或方法'assign'

Error for 2 : Object doesn't support property or method 'assign'

关于如何在IE 11中合并对象的任何建议?

Any suggestions on how I should merge the object in IE 11?

推荐答案

IE11不支持 Object.assign

IE11 does not support Object.assign.

您可以迭代数组和键,并将值作为结果对象的新属性。

You could iterate the array and the keys and take the values as new property of the result object.

var objs = [{ Name: "ABC" }, { Roll: 123 }],
    result =  objs.reduce(function (r, o) {
        Object.keys(o).forEach(function (k) {
            r[k] = o[k];
        });
        return r;
    }, {});

console.log(result);

这篇关于如何在IE 11中合并对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆