1.往Object里面添加元素
const obj = {timeArr: [],fileIds: [],isVerifyOrder: res?.isVerifyOrder ? "true" : "false",isCompose: res?.isCompose ? "true" : "false",};Object.assign(res, obj);//往res添加属性const obj = Object.assign({}, this.orderInfo, this.addressObj);obj.orderChannelType = 4; // 订单渠道obj.receivableMoney = String(this.actualMoney); // 应收金额obj.invoiceId = this.invoiceInfo?.id; // 关联发票
2.排除Object中不需要的键值
const obj = {a:1,b:2,c:3,d:4
}// 我们想要获取除了a之外的所有属性
const {a, ...other} = obj
3.对象快速求和
const objs = [
{name:'lilei', score: 98},
{name:'hanmeimei', score: 95},
{name:'polo', score: 85},
...
]const scoreTotal = objs.reduce( (total, obj) => {return obj.score + total;
}, 0 /*第二个参数是total的初始值*/)
4.利用Object.assign初始化数据
//初始化所有data数据
Object.assign(this.$data, this.$options.data());//初始化choiceList这个数据
Object.assign(this.$data.choiceList, this.$options.data());