FCC - JS - Destructuring assignment
by vanduzled
JavaScript
//Destructuring assignment is special syntax introduced in ES6, for neatly assigning values taken directly from an object.
const HIGH_TEMPERATURES = {
yesterday: 75,
today: 77,
tomorrow: 80
};
// Only change code below this line
//from
//const today = HIGH_TEMPERATURES.today;
//const tomorrow = HIGH_TEMPERATURES.tomorrow;
//to:
const {today, tomorrow} = HIGH_TEMPERATURES;
// Only change code above this line
//Use Destructuring Assignment to Assign Variables from Objects
const HIGH_TEMPERATURES = {
yesterday: 75,
today: 77,
tomorrow: 80
};
// Only change code below this line
const {today: highToday, tomorrow: highTomorrow} = HIGH_TEMPERATURES;
// Only change code above this line
//Use Destructuring Assignment to Assign Variables from Nested Objects
const LOCAL_FORECAST = {
yesterday: { low: 61, high: 75 },
today: { low: 64, high: 77 },
tomorrow: { low: 68, high: 80 }
};
// Only change code below this line
const {
today: {low: lowToday, high: highToday },
tomorrow: {low: lowTomorrow, high: highTomorrow }
} = LOCAL_FORECAST;
// Only change code above this line
console.log(lowTomorrow);
//use destructuring assignment with the rest parameter to reassign array elements
const source = [1,2,3,4,5,6,7,8,9,10];
function removeFirstTwo(list) {
// Only change code below this line
const [a,b,...arr] = list;
// Only change code above this line
return arr;
}
const arr = removeFirstTwo(source);
console.log(arr);
// [ 3, 4, 5, 6, 7, 8, 9, 10 ]
//Use Destructuring assignment to pass an object as a function's parameters
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
console.log(half(stats));
//28.05
//Create Strings Using Template Litereals
const result = {
success: ["max-length", "no-amd", "prefer-arrow-functions"],
...