These two version isn't 100% the same, when value is null
the result is different:
// version 1
const dogs = animal.dogs || [];
const cats = animal.cats || [];
// version 2
const { dogs = [], cats = [] } = animal;
Since version 1 is a lot tedious, so I want to know is there any concise way to archive this effect.
To be more specific, assumming that animal
comes from api, and its value could be animal: { dogs: ['some kind of dog'], cats: ['some kind of cat'] }
or animal: { dogs: null, cats: null }
I want to assign value []
to dog
or cat
when its value is null