Search⌘ K
AI Features

Solution Review: Destructuring

Explore how to apply JavaScript destructuring to extract properties from nested objects and arrays. This lesson helps you understand the syntax and logic behind accessing specific values, improving your ability to write concise and readable code for common interview problems.

We'll cover the following...

Solution

Javascript (babel-node)
function display(){
const exampleObject = {collection: [{name: "Kelly",}, {name: "Anna",}],}
const { collection: [,{name: secondObject,}] } = exampleObject
console.log(secondObject)
}
display()

Explanation

This problem has a one-line solution. Let’s start by discussing what exampleObject contains:

const exampleObject = {
   collection : [
                  {name: "kelly",},
                  {name: "Anna",}
                ],
}

As you can see, it contains a parent collection object which contains an array ...