-1

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

enter image description here

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

Nazehs
  • 478
  • 1
  • 10
  • 19
  • *but it seems not working* **is not an adequate problem description.** Are you getting any errors in the console? If not, how does what you're getting differ from what you want? Please **never** post images of code. Add sample data to your question. http://idownvotedbecau.se/imageofcode – connexo Dec 07 '21 at 02:23
  • @connexo I did add my sample code and I don't know which sample code you are referring to in your comment. The image is my response and it's quite clear. My react app can't run once I do what I shared in the code above. – Nazehs Dec 07 '21 at 19:47

1 Answers1

1
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?

fixiabis
  • 363
  • 1
  • 8