ES6+对象常用方法

1.往Object里面添加元素

javascript 复制代码
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中不需要的键值

javascript 复制代码
const obj = {
    a:1,
    b:2,
    c:3,
    d:4
}

// 我们想要获取除了a之外的所有属性
const {a, ...other} = obj

3.对象快速求和

javascript 复制代码
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初始化数据

javascript 复制代码
//初始化所有data数据
Object.assign(this.$data, this.$options.data());

//初始化choiceList这个数据
Object.assign(this.$data.choiceList, this.$options.data());
相关推荐
社恐的下水道蟑螂几秒前
LangChain 进阶实战:从玩具 Demo 到生产级 AI 应用(JS/TS 全栈版)
前端·langchain·openai
Fairy要carry3 分钟前
项目01-手搓Agent之loop
前端·javascript·python
亲亲小宝宝鸭8 分钟前
Ctrl ACV工程师的提效之路:删掉项目中的冗余
前端
kyriewen11 分钟前
DOM树与节点操作:用JS给网页“动手术”
前端·javascript·面试
米饭同学i14 分钟前
基于腾讯云COS的小程序素材上传功能实现
前端·javascript·react.js
cxxcode15 分钟前
前端性能指标接入 Prometheus 技术方案
前端
辣椒炒代码15 分钟前
🚀 AI Agent 入门实战:基于 LangChain + MCP 构建智能导游助手
前端
ruanCat20 分钟前
前端工程化工具链从零配置:simple-git-hooks + lint-staged + commitlint
前端·git·代码规范
光影少年22 分钟前
如何开发一个CLI工具?
javascript·测试工具·前端框架·node.js
哈__32 分钟前
ReactNative项目OpenHarmony三方库集成实战:react-native-fingerprint-scanner
javascript·react native·react.js