Multiple variables set to req.body?

Hey all, I could not figure out some of the Boss Machine project - and so looking at the solution code, I noticed a syntax I’d never seen before. Below is the code:

const checkMillionDollarIdea = (req, res, next) => {
  const { numWeeks, weeklyRevenue } = req.body;
  const totalMoney = Number(numWeeks) * Number(weeklyRevenue);
  if (!numWeeks || !weeklyRevenue || isNaN(totalMoney) || totalMoney < 1000000) {
    res.status(400).send();
  } else {
    next();
  }
}

In particular, its const { numWeeks, weeklyRevenue } = req.body;

What is this actually doing? Is it a quick way of setting two variables to the same value? Also, would

const numWeeks = req.body;
const weeklyRevenue = req.body;

have achieved the same goal?

Thanks!

its explained here:

https://hacks.mozilla.org/2015/05/es6-in-depth-destructuring/

under destructing objects.

no, then the code would have to be:

const numWeeks = req.body.numWeeks;
const weeklyRevenue = req.body.weeklyRevenue;

then we can see the destructing assignment does save us a lot of typing!

1 Like