Object.fromEntries
- 接收 Object.entries 返回的数组 再转成对象
- 生成的对象和原对象不是同一个对象(返回的是个新对象)
- 也能接收一个map转成对象
var obj = {
a: 1,
b: 2
}
Object.prototype.c = 3;
var objArr = Object.entries(obj);
var newObj = Object.fromEntries(objArr);
console.log(obj === newObj);
var obj = {
a: 1,
b: 2
}
Object.prototype.c = 3;
var objArr = Object.entries(obj);
var map = new Map(objArr);
var newObj = Object.fromEntries(map);
console.log(newObj);
实现
Object.prototype.myFromEntries = function(obj) {
var _obj = {};
for (var item of obj) {
_obj[item[0]] = item[1];
}
return _obj;
}