Skip to main content

Command Palette

Search for a command to run...

Destructuring nested object

Published
•1 min read•View as Markdown

Task: Destructuring response.body.count.

const response = {
  msg: "success",
  body: {
    count: 5,
    data: ["File 1", "File 2"],
  },
};

const {
  body: { count },
} = response;

console.log(count); // 5

Here, first body is destructured, then count is destructured from body.

More from this blog

Codedrops.tech

49 posts