简体   繁体   中英

How to destructure a nested object with special character and using object alias

I have been wondering if there is a way I can destructure this object in the snippet.

在此处输入图像描述

Here is what I have tried but it seems not working...

const {
    _links: {
      self: [{ href: mainMedia }],
    },
    _links: { "wp:attachment": attachmentMedia:{[{href:attachment}]} },
    _links: { "wp:featuredmedia": featuredMedia:{[{href:attachment}] } }
  } = data;

The attachmentMedia and featuredMedia are my alias and I am interested in the link each array.

MDN docs seems not to have this

const [{ href: mainMedia }] = data._links["self"];

const attachmentMedia = data._links["wp:attachment"]
  .map(({ href: attachment }) => ({ attachment }));

const featuredMedia = data._links["wp:featuredmedia"]
  .map(({ href: attachment }) => ({ attachment }));

Is this your expectation?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM