Andrii Yakymyshyn
Andrii Yakymyshyn

Reputation: 228

Is there way to destruct elements in js more shorter and clean?

I have this code:

const url = `https://the-cocktail-db.p.rapidapi.com/lookup.php?i=${action.id}`;

const { loading, data } = yield call(fetchData, url);
const { drinks } = data;
const [ details ] = drinks;
const { strDrink, strDrinkThumb, strInstructions, idDrink } = details;

const cocktail = { strDrink, strDrinkThumb, strInstructions, idDrink };

yield put(getCocktailDetails(cocktail, action.id, loading));

Is there way to make it more clean and short?

Can I do something like:

const [{val1, val2, val3}] = arrayOfObjects;

Upvotes: 1

Views: 90

Answers (1)

jonatjano
jonatjano

Reputation: 3738

yes you can :

const arrayOfObjects = [{val1: 1, val2: 42, val3: 18}, {val4: 54}]
const [{val1, val2, val3}, {val4}] = arrayOfObjects;
console.log(val1, val2, val3, val4)

Upvotes: 1

Related Questions