0

I have an object with the following structure:

const inventory = {
    item: {
        details: [
            {
                name: "Item A",
                type: "Type A"
            }
        ]
    }
}

I would like to access the name property with pure destructuring. So far I got to the first element in the details array.

const {item: {details: [firstDetail]}} = inventory;

I don't know where to go from here to access the name property. I was expecting it to be like the following but it doesn't work. Any ideas on how to achieve this?

const {item: {details: [firstDetail]: {name}}} = inventory;

1 Answer 1

2

Just {name} instead of firstDetail:

const inventory = {
    item: {
        details: [
            {
                name: "Item A",
                type: "Type A"
            }
        ]
    }
}

const {item: {details: [{name}]}} = inventory;

console.log(name);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.