0

The following code is giving me the output I wanted, But I need to understand more. Can anyone please explain what's happening here with spread operator,

 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"
};
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
Joseph
  • 75
  • 1
  • 6
  • 1
    This doesn't look like R code to me, so why did you use the `[spread]` tag? (Hint: *read the tag wiki!*) – Stephen C Dec 24 '20 at 06:11
  • Assume the json data return from db. Here I want to remove two columns or I don't want the properties(otherIndustry, currentOrganization). – Joseph Dec 24 '20 at 07:17
  • 2
    So you didn't take the hint. I have fixed the tag for you and added a `[javascript]` so that people who can answer are more likely to notice the question. Note: correct tagging it important if you want answers. (And if you don't want answers ... you shouldn't be asking.) – Stephen C Dec 24 '20 at 07:22

1 Answers1

2

This is more of an object destructuring assignment question.

So this line const {otherIndustry,currentOrganization, ...otherIndustry2} = myObject; is an object destructuring statement, it first assigns the value of otherIndustry and currentOrganization to the variables under the same name, for the rest of the values, they are all assigned as an object to otherIndustry2

Reference: destructuring assignment

kennysliding
  • 2,783
  • 1
  • 10
  • 31