0

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

3

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.