0

I initialize multiple variables using bracket initialization and it works successfully as you can see in console.log

let req = {
  body: {
    fname: "foo",
    address: "123 Galaxy St.",
    telnumber: "123-123-6969",
  },
};
let { fname, address, telnumber } = req.body;

console.log("fname ", fname);
console.log("address ", address);
console.log("telnumber ", telnumber);

Now I want to re initialize the value again using bracket initialization using bracket initialization but I can cannot update the value again as I get error in

{fname, address, telnumber} = newdata;

 let reqs = {
  body: {
    fname: "foo",
    address: "123 Galaxy St.",
    telnumber: "123-123-6969",
  },
};
let { fname, address, telnumber } = reqs.body;

let newdata = { fname: "new fname", address: "new Street St.", telnumber: "90-90-90" };
{fname, address, telnumber} = newdata;

console.log("fname ", fname);
console.log("address ", address);
console.log("telnumber ", telnumber)

How to update the value using that style again? I like to use that again as it's efficient (and I know the pros and cons of using that). I just want to know if there is a way to update that value using that style.

Stacks Queue
  • 848
  • 7
  • 18
  • 1
    you have to put it in parens: `({fname, address, telnumber} = newdata);` – gog Oct 14 '22 at 09:28
  • 1
    The problem is that the language syntax says that a `{` where a statement is expected starts a block statement, not a destructuring pattern. The solution is to put it where an expression is expected instead, typically by using `()` like this: `({fname, address, telnumber} = newdata);` See details in the linked question's answers. – T.J. Crowder Oct 14 '22 at 09:28

0 Answers0