首页 > 解决方案 > 谁能用传播运算符解释这里发生了什么

问题描述

下面的代码给了我想要的输出,但我需要了解更多。任何人都可以用传播运算符解释这里发生了什么,

 const myObject = {
      "employeeid": "160915848",
      "firstName": "tet",
      "lastName": "test",
      "email": "test@email.com",
      "country": "Brasil",
      "currentIndustry": "aaaaaaaaaaaaa",
      "otherIndustry": "aaaaaaaaaaaaa",
      "currentOrganization": "test",
      "salary": "1234567"
    };
    const {otherIndustry,currentOrganization, ...otherIndustry2} = myObject;
    console.log(otherIndustry2);

Output:

{
  "employeeid": "160915848",
  "firstName": "tet",
  "lastName": "test",
  "email": "test@email.com",
  "country": "Brasil",
  "currentIndustry": "aaaaaaaaaaaaa",
  "salary": "1234567"
};

标签: javascriptspread-syntax

解决方案


这更像是一个对象解构分配问题。

所以这一行const {otherIndustry,currentOrganization, ...otherIndustry2} = myObject;是一个对象解构语句,它首先将 和 的值赋给otherIndustry同名currentOrganization变量,其余的值都作为一个对象赋给otherIndustry2

参考:解构赋值


推荐阅读