Saturday, March 31, 2018

JavaScript Spread Operator and Destructuring

I just found out about this amazing (now rather old) feature of ES6 while googling of ways to clone an object then excluding some of the properties. We basically can do this by combining spread operator with destructuring.

So supposed we have this recipe object and we want to turn this recipe into an alcohol free recipe ie. taking the rum and kirsch out of the recipe.

Output:

   Original recipe: {"egg":45,"butter":60,"sugar":150,"rum":10,"kirsch":10,"milk":120,"flour":150}
   Alcohol free recipe: {"egg":45,"butter":60,"sugar":150,"milk":120,"flour":150}