DEV Community

loading...

Use Destructuring Assignment to Pass an Object as a Function's Parameters

rthefounding profile image Randy Rivera ・1 min read

In some cases, you can destructure the object in a function argument itself.
Let's use destructuring assignment within the argument to the function half to send only max and min inside the function.

const stats = {
  max: 56.78,
  standard_deviation: 4.34,
  median: 34.54,
  mode: 23.87,
  min: -0.75,
  average: 35.85
};

const half = ({ max, min }) => (max + min) / 2.0;
Enter fullscreen mode Exit fullscreen mode
console.log(half(stats)); will display 28.015
Enter fullscreen mode Exit fullscreen mode

Discussion (0)

Forem Open with the Forem app