本节知识点
- 对象赋值
- 对象key值构建
- object.assign() 对象合并
(一)对象赋值
ES6中允许我们把声明的变量直接赋值给对象。
let name = "张三";
let age = 28;
let json = {name,age};
console.log(json);
(二) 对象KEY值构建
let key = "name";
let key2 = "age";
let json = {
[key]:"张三",
[key2]: 26
};
console.log(json);
输出的结果就是 {name:张三,age:26}
(三) object.assign()合并对象
操作数组的时候合并数组。对象也有合并的方法。那就是assign()
数组的合并
let arr = [1,2,3,4,5];
let arr2 = [6,7,8];
let result = arr.concat(arr2);
console.log(result);
对象的合并 Object.assign()
let a = {name:"张三"};
let b = {age:28};
let c = {hobby:"敲代码"};
let result = Object.assign(a,b,c);
console.log(result);