Skip to content Skip to sidebar Skip to footer

Destructuring In A Return Statement

I have multiple cases throughout my app that look something like this: getVariables() { const { allowCustomValues, budgets, budgetsToAdd, budgetsToRem

Solution 1:

Actually destructuring does not allow that.

You are asking about equivalent for lodash's pick(). Maybe you have already lodash in you project. If you don't you still can write such a helper on your own(but better use stable community-proved versio from lodash or another library)


Solution 2:

Why not just use Object.assign()?

    return Object.assign({}, this.props);

That'll build a new empty object and then copy all the enumerable properties from this.props into it. The target object is returned by Object.assign() so it pretty much does all the work.


Solution 3:

How about

return {...this.props}


Post a Comment for "Destructuring In A Return Statement"