DEV Community

loading...

Destructuring nested object

Mehul Lakhanpal
Full stack Developer | Micro-blogging on Web Development at codedrops.tech | Creator of File Ops, Note Box, GitHub Marker
Originally published at codedrops.tech ・1 min read

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
Enter fullscreen mode Exit fullscreen mode

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


Thanks for reading 💙

Follow @codedrops.tech for daily posts.

InstagramTwitterFacebook

Micro-Learning ● Web Development ● Javascript ● MERN stack ● Javascript

codedrops.tech

Discussion (2)

Collapse
janpauldahlke profile image
jan paul

sweet ecmascript. i find you fluff and beatifull.
except undefined, NaN and null...

Collapse
saroj8455 profile image
Saroj Padhan

Thank you