Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

190
Views
Assign default value to object if value is undefined

I'm fetching data from an API, some fields are not required therefore undefined in the response object

I would like to assign a default empty value like null '' [] to my object parameters if said fields are undefined in the response, so far here's what I have (I have more more fields to fetch but stopped at 5 for this example)

await axios.get('https://myurl.com').then( async (response) => {
    await Promise.all(response.data.map(async (commerce) => {

      let options = {
        // BASE INFO
        field1: commerce.field1[0].value,
        field2 : commerce.field2[0].value,
        field3 : commerce.field3[0].value,
        field4 : commerce.field4[0].value,
        field5 : commerce.field5[0].value,
      }

      const default_options = {
        // BASE INFO
        field1: null,
        field2 : '',
        field3 : '',
        field4 : [],
        field5 : null,
      }

      const commerceObj = Object.assign({}, default_options, options)
    }));

  });

I get an error if a field has no value for instance commerce.field2[0] is undefined because the API response doesn't include it, what I'm looking for would be an efficient way to do something like

field2 : if(commerce.field2[0].value) { commerce.field2[0].value } else {''}

Since I have a lot of fields I don't really wish to do have a lot of if in my code the only thing I could find that could help me is Object.assign() but it didn't help much

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

You can use Conditional (ternary) operator.

Syntax: condition ? exprIfTrue : exprIfFalse

  let options = {
    field1: commerce.field1[0].value ? commerce.field1[0].value : '',
    ...
  }
about 4 years ago · Santiago Trujillo Report

0

Have a look into using @babel/plugin-proposal-optional-chaining this would allow you to do something such as:

const field1 = commerce?.field1[0]?.value

You could also look into _.get:

const field1 = _.get(commerce, 'field[0].value', null)

If not you'll have to do something a little more cluttered:

commerce && commerce.field1 && commerce.field1.length
  ? commerce.field1[0].value
  : undefined
about 4 years ago · Santiago Trujillo Report

0

the idea you laking is to loop the first object and assign your default options dynamically

let options = {
        // BASE INFO
        field1: commerce.field1[0].value,
        field2 : commerce.field2[0].value,
        field3 : commerce.field3[0].value,
        field4 : commerce.field4[0].value,
        field5 : commerce.field5[0].value,
      }
const default_options = {
        // BASE INFO
        field1: null,
        field2 : '',
        field3 : '',
        field4 : [],
        field5 : null,
      }
for (const fieldKey in options) {
  if(!options[fieldKey])
    options[fieldKey] = default_options[fieldKey] // dynamically change it
}
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!