1

Here is a snippet of my code

const data = [ ...prev, ...resp.data ]

This crashes if resp.data is not defined. With the new chaining operator syntax I have found following solution how to handle the case that resp.data is undefined.

const data = [ ...prev, ...( resp?.data || [] ) ]

It works fine. I just wonder if there is some shorter syntax.

VLAZ
  • 26,331
  • 9
  • 49
  • 67
Jirmed
  • 373
  • 2
  • 13
  • Single character shorter: `const data = [].concat( prev, resp?.data || [] )` but it doesn't use spread syntax any more. – VLAZ Feb 18 '21 at 10:16
  • Oops, turns out I cannot count. The above is *longer* but has less spaces. `prev.concat( resp?.data || [] )` is two characters shorter if we ignore the whitespaces. – VLAZ Feb 18 '21 at 10:25

0 Answers0