Search⌘ K
AI Features

Solution Review: Destructuring

Explore how to apply deep destructuring to extract specific nested values from objects in JavaScript. Learn to define function parameters for nested properties and use template literals to display formatted strings. This lesson helps you master handling complex objects efficiently.

Solution

Let’s start by reviewing the solution code step-by-step.

Javascript (babel-node)
'use strict';
'use strict';
const getDetails = function({name, born: { year: birthYear }, graduated: {year}}) {
return `${name} born in the year ${birthYear}, graduated in ${year}.`;
};
const details =
getDetails({name: 'Sara',
born: { month: 1, day: 1, year: 2000 },
graduated: { month: 5, day: 31, year: 2018 }});
console.log(details);
//Sara born in the year 2000, graduated in 2018.

Explanation

The object passed as a parameter to the getDetails() function is a nested object. The output displays the three values given in that nested object.

  1. name
  2. born
...