Use Destructuring Assignment to Assign Variables from Objects HELP!

Tell us what’s happening:
Im not understanding anything there, despite the fact i really tryharded, even with the solution. It is possible that someone try to explain me what are destructuring assignment with his own words? Might help. Thanks, i stay connected.

Your code so far


const AVG_TEMPERATURES = {
  today: 77.5,
  tomorrow: 79
};

function getTempOfTmrw(avgTemperatures) {
  "use strict";
  // change code below this line
  const tempOfTomorrow = undefined; // change this line
  // change code above this line
  return tempOfTomorrow;
}

console.log(getTempOfTmrw(AVG_TEMPERATURES)); // should be 79

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-assign-variables-from-objects/

Hey @Peyo5202

As it is mentioned in the above page
Destructuring assignment is special syntax for neatly assigning values taken directly from an object to variables.

// here you are creating another variables x ,y , z and then copying the value of the object
var x = voxel.x; // x = 3.6
var y = voxel.y; // y = 7.4
var z = voxel.z; // z = 6.54

Destructing helps you reduce your code a bit as it copies and assigns the values all together
// The below code means that gather the property x, y, z from the object voxel and at the same time create new variables x,y,z and assign these value. 
const { x, y, z } = voxel; // x = 3.6, y = 7.4, z = 6.54 


But if you want to customize your name of the variable the you can follow the example mentioned below
// We use a (full colon) : and specify the name of the variable we want to create. Here we have specified copy the values of x,y,z from the object voxel and create a new variable a,b,c and store it in them. 
const { x : a, y : b, z : c } = voxel // a = 3.6, b = 7.4, c = 6.54 

Thanks for these informations. Could you maybe give me an exemple of what would have been the solution without using Destructuring assignment? I think i might be able to actually figure out the point of those, by looking closely between the 2 solutions !

Thanks

Peyo

const AVG_TEMPERATURES = {
 today: 77.5, tomorrow: 79 };
 
function getTempOfTmrw(avgTemperatures)
 { "use strict";
 // change code below this line 
 const tempOfTomorrow = avgTemperatures.tomorrow; 
// change this line 
// change code above this line return tempOfTomorrow; 
}
 console.log(getTempOfTmrw(AVG_TEMPERATURES));

This is the solution without using Destructuring assignment.

<redacted> //It will help you overcome challenges

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

Thank you for understanding.

1 Like