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());
//若需要给某一个具体的数据 (eg: form) 重新设置值,则使用如下的方法,eg:
Object.assign(this.$data.form, this.$options.data().form)