0

I have a function that will call an external resource (e.g. REST) and will return a JSON object depending on the result.

For instance if I send a POST and it works, I need the object to be:

{ 
    code: 201, 
    id: <new_id_added> 
}

But when it fails for some reason, I don't want the id (cause it will be undefined). Something like:

{ 
    code: 400
}

So to have one single handling I have this:

response = { 
    code: result.code,
    id: result.id 
}

Which would work when all is OK, but then when it fails it would render:

{ 
    code: 400, 
    id: undefined
}

Is there any way to make "id" be optional, depending on whether it is defined? Something like:

response = { 
    code: result.code,
    id?: result.id
}

Then on "undefined" it will just be left out? I know I can just take the object and run a filter later to remove undefined attributes, but I think this would be just overkill, given that most of the times the response will be successful

Charlie
  • 22,886
  • 11
  • 59
  • 90
Antonio Ortells
  • 327
  • 2
  • 11
  • 1
    There's no clean way to do it in a single object declaration IMO, I think the best you can do is to assign to `response.id` inside an `if` after declaring `response` – CertainPerformance Sep 06 '19 at 08:28
  • Quick solution `{ code: result.code, ...(result?.id && { id: result.id }) }` – Asaf Aug 01 '22 at 15:42

2 Answers2

2

You can use spread operator.

response = {...result}

This will create a new object holding values that exist in result object.


UPDATE

//--------------------------------------------
// First Example
//--------------------------------------------

// response can have other properties as well
var response = {
  foo: 'foo',
  bar: 'bar'

}

// Has no ID
var resultWithoutID = {
  code: 400
}


response = {...response, ...resultWithoutID};
console.log('Response without ID: ', response);

//--------------------------------------------
// Second Example
//--------------------------------------------

response = {
  foo: 'foo',
  bar: 'bar'

}

// Has ID
var resultWithID = {
  code: 400,
  id: 123
}


response = {...response, ...resultWithID};
console.log('Response with ID: ', response);
Harun Yilmaz
  • 8,281
  • 3
  • 24
  • 35
  • This does not seem to work: var result={ code: 400 } var finalResult= { code: result['code'], id: result['id'] } var finalSpread= { ...finalResult } console.log(finalSpread); Renders: { code: 400 , id: undefined } – Antonio Ortells Sep 06 '19 at 11:33
  • It's because you're assigning 'id' manually by doing `id: result['id']`. I will update the answer to give an example – Harun Yilmaz Sep 06 '19 at 11:37
  • OK maybe I explained it wrong. I am not reusing the object that comes from the REST. I am manually building a new object that has as a code the HTTP result code (just a number, not a JSON object) that came from REST, and, if there is an ID field in it, also that. – Antonio Ortells Sep 06 '19 at 12:48
0

You could use Object.assign with a check for id

response = Object.assign({ code: result.code }, 'id' in result && { id: result.id });
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • This will work but it is very specific to this case, in the sense that you will have to know beforehand what can be undefined, and if there are multiple potentially undefined values this would grow too big. I was looking for something more generic – Antonio Ortells Sep 06 '19 at 11:32