js合并对象常用方法

const person = { name: 'David Walsh', gender: 'Male' };
const tools = { computer: 'Mac', editor: 'Atom' };
const attributes = { handsomeness: 'Extreme', hair: 'Brown', eyes: 'Blue' };

方法

一、ES6扩展运算符

const summary = {...person, ...tools, ...attributes};
console.log(summary); 

输出结果:

二、Object.assign() 方法用于将所有可枚举属性的值从一个或多个源对象复制到目标对象。它将返回目标对象。

var newObj  = Object.assign(person, tools, attributes);
console.log(newObj);

输出结果:

 

猜你喜欢

转载自www.cnblogs.com/liushihong21/p/11275719.html
今日推荐