Destructuring on Objects

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

// Only change code below this line
const half = ({max, min}) => (max + min) / 2.0; 
// Only change code above this line
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36

Challenge: Use Destructuring Assignment to Pass an Object as a Function’s Parameters

Link to the challenge:

I don’t get why we should use the curly brackets between the inputs:




How do we know that it will input the ‘max’ and ‘min’ values from the ‘stats’ object. What if there were multiple objects which included this property? Wouldn’t that mess up the result?

I meant to talk about this one cause it’s surrounded by curly brackets, don’t know if it is an input field or not but I think it is.

So long as the object being passed contains a max and a min, the function will work. Objects don’t worry about the order of properties, simply the names of the property.

It’s the function call half(stats) that determines which object is supplied to it. You just do not see the function call in the code provided but you can see it in the tests at the bottom left.

This is a parameter, and particularly an object being passed as a parameter. We are able to pull particular properties by name out of that object, without needing to know their order.

Oh, I didn’t notice that. Thought it would have to be written in the code for it to work. Thanks for the help, sir!

No, I meant like what if there was another object like this in the code:

const fake-stats = {
max: 10;
min: 1;

Now, how do we know that the function will select the properties from the actual stats object? Because, it doesn’t specify anywhere in the code that we are targeting that specific object

Sure does - we’re passing our object as the parameter:

console.log(half(fake_stats) );
1 Like

Ohhh, I get it now. We are inputting the object into the function and the values of that object are passed as ‘max’ and ‘min’ cause of the destructuring.
I guess I didn’t knew how destructuring worked all along. Thanks, dude :+1:

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.