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

2
  • 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 Commented Sep 6, 2019 at 8:28
  • Quick solution { code: result.code, ...(result?.id && { id: result.id }) } Commented Aug 1, 2022 at 15:42

2 Answers 2

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);

Sign up to request clarification or add additional context in comments.

3 Comments

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 }
It's because you're assigning 'id' manually by doing id: result['id']. I will update the answer to give an example
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.
0

You could use Object.assign with a check for id

response = Object.assign({ code: result.code }, 'id' in result && { id: result.id });

1 Comment

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.