Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to destructure an array/object

I want to destructure this but i can’t get it (I need the vcardArray)

I can’t get it with .vcardArray or with const { vcardArray } = *variablename

[
    { 
        objectClassName: 'entity', 
        handle: '292', 
        roles: ['registrar'], 
        publicIds: [[Object]], 
        vcardArray: ['vcard', [Array]], 
        entities: [[Object]]
    }
]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Destructuring syntax is exactly the same as object and array literal syntax,¹ it’s just interpreted the other way around (and it’s more common for us to use the shorthand {name} form than the full {name: name} form, though both are used).

So if you think about how you’d create an array with an object with a vcardArray property, that’s how you destructure it.

const [ { vcardArray } ] = /*...your object...*/;

¹ With one caveat: The innermost tokens have to be assignable targets, so they can’t be literals. {example: 42} is a valid object literal (creating an object with an example property and setting that property to 42), but you can’t use it as a destructuring pattern because it would mean "get the example property and assign it to 42" you can’t assign to the literal 42.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading