TransWikia.com

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

Stack Overflow Asked by Andrii Yakymyshyn on December 9, 2021

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;

One Answer

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)

Answered by jonatjano on December 9, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP