How the function know form which object i want to do destructuring?

Hey!
let’s say i have another object with similar keys, how the half function knows from which object i want to take the min & max values?

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 = ({min,max}) => (max + min) / 2.0;
// Only change code above this line
;

1st it will not have same key within that obj, if it’s a nested obj than you have to do deep destructuring (i.g. const { main: { content: { title } } } = obj) , and if it is a array of object u need to use iterator to iterate for value of each key and cheke by condition for what u want

i mean another object with similar keys to the stats object, i.e:
const abcd = {
min: 10
}

see this code

const people = [
  {
    name: 'Mike Smith',
    family: {
      mother: 'Jane Smith',
      father: 'Harry Smith',
      sister: 'Samantha Smith'
    },
    age: 35
  },
  {
    name: 'Tom Jones',
    family: {
      mother: 'Norah Jones',
      father: 'Richard Jones',
      brother: 'Howard Jones'
    },
    age: 25
  }
];

for (const {name: n, family: {father: f}} of people) {
  console.log('Name: ' + n + ', Father: ' + f);
}

// "Name: Mike Smith, Father: Harry Smith"
// "Name: Tom Jones, Father: Richard Jones"
1 Like

check tha code that i have sent, if it is not nested obj than it is array of obj… if its array you need to pullout all key and value from array

The destructuring will work on any object with the min and max keys. That’s part of the power and flexibility of destructuring.

The function will use the object it gets passed as an argument. It doesn’t “know” anything.

1 Like

i know that the function doesn’t “know” anything.
in my code you can see that i didn’t specify any object, i added the values as an arguments
but those values can be related to any other object…
the arguments for the function were {min,max} & can be taken from any other object.
let’s say i have another object named lamps which also holds min & max values how the function decides form which object the values will be taken form.
i will just note that i took the code from the solution…

i.e const half = ({min,max})

see the full code bellow

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 = ({min,max}) => (max + min) / 2.0;
// Only change code above this line
;

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

Right. It is on purpose that destructuring will work for any object with a min and max attribute.

That is not the full code. You are missing the actual call to the function which is where the object you want to use is passed in. The function will use the object you pass to it, destructuring the properties as specified.

1 Like

You’re not the first one to be confused about this. If you look at the lower left of the screen, you’ll see that the function is called by the tests with the stats object as parameter:

half(stats)

That’s how the function knows which object to destructure.

1 Like

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