By hussain

2019-05-15 16:28:08 8 Comments

Below I have added patientId and inusranceId to spread operator , just wanted to see if there is better approach to achieve this task ?


const Response = await getData(re);
const patientId = "AW364747";
const insuranceId = "0000786622";

logger().info('Response=', { ...Response, drugName: '[REDACTED]' , patientId, insuranceId});


@GifCo 2019-05-15 16:34:34

What you have is correct.

let newResponse = {...Response, drugName: '[REDACTED]'}

Will either add drugName property if it is missing from Response. OR it will override the drugName property if it is in Response.

let newResponse = { drugName: 'defaultname', ...Response}

will do the opposite, if Response contains drugName it will override drugName: 'defualtname' or if it doesn't exist in Response it will add drugName.


const Response = { prop: 'some prop', drugName: 'Original Drug'}

const spreadFirst = { ...Response, drugName: 'New drug'}
// spreadFirst = { prop: 'some prop', drugName: 'New drug'}

const spreadLast = { drugName: 'Newest Drug', ...Response}
// spreadLast = { prop: 'some prop', drugName: 'Original Drug'}

@hussain 2019-05-15 16:49:00

I dont see any difference in both of your above approaches output would be same right

@hussain 2019-05-15 16:50:12

my questions was if we add any property where it would be added , would it add new properties at same level as others like drugName in the Response object

@GifCo 2019-05-15 16:54:14

@hussain so first if spread comes first whatever follows will replace properties in Response. If spread comes last then Response will replace whatever is before it. drugName will be at the same level as all the properties within Response. Just as if you simply Response.drugname = "drugname"

@GifCo 2019-05-15 16:59:25

added an example to make it more clear.

@Kamil Kiełczewski 2019-05-15 16:39:25

There is more clear and direct approach

let Response = { name: 'John' }; // await getData(re);

Response.drugName    = '[REDACTED]'; 
Response.patientId   = 'AW364747';
Response.insuranceId = '0000786622';


@GifCo 2019-05-15 16:41:17

The question specifically asks to use spread operator. This also mutates Response which may not be desirable.

@Kamil Kiełczewski 2019-05-15 16:44:10

There something like "spread operator" doesnt exists - there is spread syntax and we cannot add anything to it. OP not mention that mutations are not acceptable

@GifCo 2019-05-15 16:47:36

The spread syntax uses the (...) operator Either way its semantics and the question clearly called for the spread syntax/operator .

Related Questions

Sponsored Content

86 Answered Questions

[SOLVED] How do JavaScript closures work?

3 Answered Questions

58 Answered Questions

[SOLVED] How do I redirect to another webpage?

41 Answered Questions

[SOLVED] Sort array of objects by string property value

12 Answered Questions

[SOLVED] How can I add new array elements at the beginning of an array in Javascript?

  • 2011-11-10 00:35:22
  • Moon
  • 678691 View
  • 1369 Score
  • 12 Answer
  • Tags:   javascript arrays

38 Answered Questions

[SOLVED] How do I remove a property from a JavaScript object?

53 Answered Questions

[SOLVED] How do I check if an element is hidden in jQuery?

79 Answered Questions

[SOLVED] How do I remove a particular element from an array in JavaScript?

  • 2011-04-23 22:17:18
  • Walker
  • 5713387 View
  • 7106 Score
  • 79 Answer
  • Tags:   javascript arrays

35 Answered Questions

[SOLVED] What is the !! (not not) operator in JavaScript?

  • 2009-04-24 08:13:58
  • Hexagon Theory
  • 463042 View
  • 2702 Score
  • 35 Answer
  • Tags:   javascript operators

Sponsored Content